I know that there are several answers on this site, and I apologize if this is repeated to some extent, but all those that I found do not do what I am trying to do.
I am trying to provide method information so that I can get the name in a safe way without using strings. So I'm trying to extract it with an expression.
Let's say I want to get the method name in this interface:
public interface IMyInteface { void DoSomething(string param1, string param2); }
Currently, I can get the name using THIS method:
MemberInfo GetMethodInfo<T>(Expression<Action<T>> expression) { return ((MethodCallExpression)expression.Body).Method; }
I can call the helper method as follows:
var methodInfo = GetMethodInfo<IMyInteface>(x => x.DoSomething(null, null)); Console.WriteLine(methodInfo.Name);
But I'm looking for a version in which I can get the method name without specifying parameters (null, null)
like this:
var methodInfo = GetMethodInfo<IMyInteface>(x => x.DoSomething);
But all attempts are not compiled
Is there any way to do this?
c # lambda expression
Andre Nov 22 '11 at 10:28 2011-11-22 10:28
source share