I am trying to write a method that returns a methodinfo class from a given input, with a view to creating the generic version of that method…i.e.
var m = myClass.GetType().GetMethod("SomeMethod").MakeGenericMethod(...blahblah..);
This works and is all good, except that I have the string literal of my method name, so if in the course of re-factoring I happen to rename one of the methods I am using I don’t find out until run time.
What I would like to do is create a helper method that I can pass a lamba to that specifies the methodgroup, that way I would get compile time checking of the method name, not to mention intellisense etc…ie.
MethodInfo mi = myClass.GetMethodInfo( o => o.SomeMethod );
m = mi.MakeGenericMethod(..blah...);
But I haven’t been able to figure out the method signature of the helper…
public MethodInfo GetMethodInfo(Func<MyClass,XXXX> lambda){ //What is my XXXX ? }
You can do it like this, but I am stumped as to why you need the to specify the delegate type. Without that it doesn’t work, but since the below works for
BarMethod, it doesn’t seem to matter what the delegate type is: