Click here to Skip to main content
15,900,816 members
Home / Discussions / C#
   

C#

 
AnswerRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
PIEBALDconsult30-Mar-10 5:04
mvePIEBALDconsult30-Mar-10 5:04 
GeneralRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
blackblizzard30-Mar-10 5:14
blackblizzard30-Mar-10 5:14 
GeneralRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
PIEBALDconsult30-Mar-10 7:35
mvePIEBALDconsult30-Mar-10 7:35 
GeneralRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
blackblizzard30-Mar-10 8:00
blackblizzard30-Mar-10 8:00 
GeneralRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
PIEBALDconsult30-Mar-10 8:42
mvePIEBALDconsult30-Mar-10 8:42 
GeneralRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
blackblizzard30-Mar-10 8:48
blackblizzard30-Mar-10 8:48 
GeneralRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
PIEBALDconsult30-Mar-10 9:49
mvePIEBALDconsult30-Mar-10 9:49 
AnswerRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
PIEBALDconsult30-Mar-10 10:24
mvePIEBALDconsult30-Mar-10 10:24 
Aha! It is documented:

"
Return Value
Type: System..::.Object

An object containing the return value of the invoked method, or nullNothingnullptra null reference (Nothing in Visual Basic) in the case of a constructor.

"

So I think you have to cast.


Here's a little something I whipped up to show a generic method to do it:

private static readonly System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodBase> dic ;

static Test
(
)
{
    dic = new System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodBase>() ;

    return ;
}

public static T
Clone<T>
(
    this T Source
)
where T : class
{
    System.Type t = typeof(T) ;

    System.Reflection.MethodBase m ;

    if ( !dic.ContainsKey ( t ) )
    {
        System.Type[] part = new System.Type[] { t } ;

        m = t.GetMethod ( "Clone" , part ) ;

        if ( ( m == null ) || ( ((System.Reflection.MethodInfo) m).ReturnType != t ) )
        {
            m = t.GetConstructor ( part ) ;

            if ( m == null )
            {
                throw ( new System.Exception ( "blah blah blah" ) ) ;
            }
        }

        dic [ t ] = m ;
    }
    else
    {
        m = dic [ t ] ;
    }

    T result ;

    if ( m.IsConstructor )
    {
        result = (T) ((System.Reflection.ConstructorInfo) m).Invoke ( new object[] { Source } ) ;
    }
    else
    {
        result = (T) m.Invoke ( Source , new object[] { Source } ) ;
    }

    return ( result ) ;
}

GeneralRe: Calling MethodBase's Invoke on a constructor (reflection) Pin
blackblizzard30-Mar-10 22:07
blackblizzard30-Mar-10 22:07 
QuestionTotally custom windows forms Pin
r0seBa29-Mar-10 19:59
r0seBa29-Mar-10 19:59 
QuestionMail [modified] Pin
ravanth29-Mar-10 18:25
ravanth29-Mar-10 18:25 
AnswerRe: Mail Pin
PSK_29-Mar-10 18:37
PSK_29-Mar-10 18:37 
AnswerRe: Mail Pin
Abhinav S29-Mar-10 19:53
Abhinav S29-Mar-10 19:53 
Question32 bit dll on 64 bit Windows --- Windows Service? Pin
devvvy29-Mar-10 17:01
devvvy29-Mar-10 17:01 
AnswerRe: 32 bit dll on 64 bit Windows --- Windows Service? Pin
PSK_29-Mar-10 17:21
PSK_29-Mar-10 17:21 
AnswerRe: 32 bit dll on 64 bit Windows --- Windows Service? Pin
Bernhard Hiller30-Mar-10 3:17
Bernhard Hiller30-Mar-10 3:17 
Questioncheck for .doc application Pin
Jassim Rahma29-Mar-10 12:23
Jassim Rahma29-Mar-10 12:23 
AnswerRe: check for .doc application Pin
PSK_29-Mar-10 17:13
PSK_29-Mar-10 17:13 
QuestionStart adobe reader maximized Pin
Jassim Rahma29-Mar-10 12:21
Jassim Rahma29-Mar-10 12:21 
AnswerRe: Start adobe reader maximized Pin
Andrew Rissing29-Mar-10 15:20
Andrew Rissing29-Mar-10 15:20 
AnswerRe: Start adobe reader maximized Pin
Som Shekhar29-Mar-10 17:02
Som Shekhar29-Mar-10 17:02 
Questionbind textbox to XML node Pin
Jassim Rahma29-Mar-10 12:00
Jassim Rahma29-Mar-10 12:00 
QuestionReusable TableAdapters and such for Lookup Tables? Pin
Bert Edens29-Mar-10 11:40
Bert Edens29-Mar-10 11:40 
QuestionReading message Id's from a file?!? Pin
MWRivera29-Mar-10 11:13
MWRivera29-Mar-10 11:13 
AnswerRe: Reading message Id's from a file?!? Pin
Not Active29-Mar-10 12:12
mentorNot Active29-Mar-10 12:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.