How to use Reflection to use method as an argument

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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)";
 
Tim Johnson said:
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.
 
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
 
Tim Johnson said:
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.
 
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"?
 
Understood, thanks!

Jon Skeet said:
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.
 
Tim Johnson said:
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.
 
Thanks again!

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