|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to use Reflection to use method as an argumentI 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 <TimJohn***@discussions.microsoft.com> wrote:
Show quote > I know how to use Reflection to instantiate a class from an assembly and then It's not entirely clear what you're after, but I *think* you want > 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)"; 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 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 > Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
Show quote > More specifically I need something along these lines: The type passed as the first parameter should be the type of the > > 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); 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 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 > 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 > Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> Related to this discussion, how would I get a return value from a reflected Use Type.GetProperty to get a PropertyInfo, then either call GetValue > 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"? 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 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 > |
|||||||||||||||||||||||