Home All Groups Group Topic Archive Search About

How to use Reflection to use method as an argument

Author
19 Apr 2006 6:31 PM
Tim Johnson
I know how to use Reflection to instantiate a class from an assembly and then
call it via InvokeMember.  But now I need to use that method as an argument
to pass into an internal routine I have that saves the method reference as a
delegate.  In non-Reflection code I'd do this:

public delegate void myHandler(string arg);

public void myMethod(myHandler mh) {...}

public void myCustomHandler(string arg) {...}

public void setup()
{
    myMethod(this.MyCustomHandler);
}

So what I'd like to do instead is sort of call "myMethod(reflectionMethod)";

Author
19 Apr 2006 6:39 PM
Jon Skeet [C# MVP]
Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
Show quote
> I know how to use Reflection to instantiate a class from an assembly and then
> call it via InvokeMember.  But now I need to use that method as an argument
> to pass into an internal routine I have that saves the method reference as a
> delegate.  In non-Reflection code I'd do this:
>
> public delegate void myHandler(string arg);
>
> public void myMethod(myHandler mh) {...}
>
> public void myCustomHandler(string arg) {...}
>
> public void setup()
> {
>     myMethod(this.MyCustomHandler);
> }
>
> So what I'd like to do instead is sort of call "myMethod(reflectionMethod)";

It's not entirely clear what you're after, but I *think* you want
Delegate.CreateDelegate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
19 Apr 2006 8:29 PM
Tim Johnson
More specifically I need something along these lines:

Assembly asy = Assembly.LoadFrom("some.dll");
Type objType = asy.GetType("someclass", true, true);
object myClass = Activator.CreateInstance(objType);

//Not this:
//myClass.InvokeMethod("myCustomHandler"...)

//But this: somehow pass in the reflected method as a delegate:
??? Method myH = myClass.GetMethod("myCustomHandler");
??? mySetupMethod(myH);

Maybe that last couple lines should be this, using your idea?:

Delegate dlg = Delegate.CreateDelegate(
                          typeof(myMethod), myClass, "myCustomHandler);
mySetupMethod(dlg);

Tim

Show quote
"Jon Skeet [C# MVP]" wrote:

> Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> > I know how to use Reflection to instantiate a class from an assembly and then
> > call it via InvokeMember.  But now I need to use that method as an argument
> > to pass into an internal routine I have that saves the method reference as a
> > delegate.  In non-Reflection code I'd do this:
> >
> > public delegate void myHandler(string arg);
> >
> > public void myMethod(myHandler mh) {...}
> >
> > public void myCustomHandler(string arg) {...}
> >
> > public void setup()
> > {
> >     myMethod(this.MyCustomHandler);
> > }
> >
> > So what I'd like to do instead is sort of call "myMethod(reflectionMethod)";
>
> It's not entirely clear what you're after, but I *think* you want
> Delegate.CreateDelegate.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Author
19 Apr 2006 9:15 PM
Jon Skeet [C# MVP]
Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
Show quote
> More specifically I need something along these lines:
>
> Assembly asy = Assembly.LoadFrom("some.dll");
> Type objType = asy.GetType("someclass", true, true);
> object myClass = Activator.CreateInstance(objType);
>
> //Not this:
> //myClass.InvokeMethod("myCustomHandler"...)
>
> //But this: somehow pass in the reflected method as a delegate:
> ??? Method myH = myClass.GetMethod("myCustomHandler");
> ??? mySetupMethod(myH);
>
> Maybe that last couple lines should be this, using your idea?:
>
> Delegate dlg = Delegate.CreateDelegate(
>                           typeof(myMethod), myClass, "myCustomHandler);
> mySetupMethod(dlg);

The type passed as the first parameter should be the type of the
delegate you wish to create. I would suggest getting the MethodInfo in
the normal way, and then using the overload of CreateDelegate which
accepts a MethodInfo (and optionally a target object). You'll then need
to cast the result to the appropriate delegate type, and *then* you'll
be able to call your other method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
19 Apr 2006 9:21 PM
Tim Johnson
Understood, thanks!

Show quote
"Jon Skeet [C# MVP]" wrote:

> Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> > More specifically I need something along these lines:
> >
> > Assembly asy = Assembly.LoadFrom("some.dll");
> > Type objType = asy.GetType("someclass", true, true);
> > object myClass = Activator.CreateInstance(objType);
> >
> > //Not this:
> > //myClass.InvokeMethod("myCustomHandler"...)
> >
> > //But this: somehow pass in the reflected method as a delegate:
> > ??? Method myH = myClass.GetMethod("myCustomHandler");
> > ??? mySetupMethod(myH);
> >
> > Maybe that last couple lines should be this, using your idea?:
> >
> > Delegate dlg = Delegate.CreateDelegate(
> >                           typeof(myMethod), myClass, "myCustomHandler);
> > mySetupMethod(dlg);
>
> The type passed as the first parameter should be the type of the
> delegate you wish to create. I would suggest getting the MethodInfo in
> the normal way, and then using the overload of CreateDelegate which
> accepts a MethodInfo (and optionally a target object). You'll then need
> to cast the result to the appropriate delegate type, and *then* you'll
> be able to call your other method.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Author
19 Apr 2006 9:20 PM
Tim Johnson
Related to this discussion, how would I get a return value from a reflected
class's Get Property?  If have a Property called "myVar" I don't think I
could just InvokeMember it - doesn't it have a different name internally as a
"method"? 

Show quote
"Jon Skeet [C# MVP]" wrote:

> Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> > I know how to use Reflection to instantiate a class from an assembly and then
> > call it via InvokeMember.  But now I need to use that method as an argument
> > to pass into an internal routine I have that saves the method reference as a
> > delegate.  In non-Reflection code I'd do this:
> >
> > public delegate void myHandler(string arg);
> >
> > public void myMethod(myHandler mh) {...}
> >
> > public void myCustomHandler(string arg) {...}
> >
> > public void setup()
> > {
> >     myMethod(this.MyCustomHandler);
> > }
> >
> > So what I'd like to do instead is sort of call "myMethod(reflectionMethod)";
>
> It's not entirely clear what you're after, but I *think* you want
> Delegate.CreateDelegate.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Author
19 Apr 2006 9:28 PM
Jon Skeet [C# MVP]
Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> Related to this discussion, how would I get a return value from a reflected
> class's Get Property?  If have a Property called "myVar" I don't think I
> could just InvokeMember it - doesn't it have a different name internally as a
> "method"? 

Use Type.GetProperty to get a PropertyInfo, then either call GetValue
directly, or if you need the getter as a MethodInfo, use GetGetMethod.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
19 Apr 2006 9:43 PM
Tim Johnson
Thanks again!

Show quote
"Jon Skeet [C# MVP]" wrote:

> Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> > Related to this discussion, how would I get a return value from a reflected
> > class's Get Property?  If have a Property called "myVar" I don't think I
> > could just InvokeMember it - doesn't it have a different name internally as a
> > "method"? 
>
> Use Type.GetProperty to get a PropertyInfo, then either call GetValue
> directly, or if you need the getter as a MethodInfo, use GetGetMethod.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

AddThis Social Bookmark Button