How to use Reflection to use method as an argument

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)";
 
J

Jon Skeet [C# MVP]

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.
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

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.
 
G

Guest

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"?
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top