Possible Duplicate:
get methodinfo from a method reference C#
This is most likely something simple but so far I have not come up with anything on how to do this.
I want to be able to get the name of a method in two different ways. Please note I want a method name, not a property name.
1) Inside of a class like ClassA<T>, looking like:
var name = GetMethodName(x => x.MethodA);
2) Outside of a class, looking like:
var name = GetMethodName<ClassA<object>>(x => x.MethodA);
var name = GetMethodName<ClassB>(x => x.MethodB);
How might I do this exactly?
Thanks!
You don’t need lambdas (
x => x.MethodA, etc). That’s just confusing the issue (and hiding the method of interest: theMethodAbit would be hidden from yourGetMethodNamemethod).Instead, you can use reflection to get a
MethodInfoobject, which then has aNameproperty.For example:
Here
methodNamewill be the string"SomeMethod". (Of course, in this simple case we’ve used the class name to get theMethodInfoobject, so it’s somewhat circular and we might as well have just used the hard-coded"SomeMethod"string instead!)