我知道网站上有一些答案,如果这有任何重复,我道歉,但我找到的所有答案都没有做我想做的事情.
我正在尝试指定方法信息,因此我可以通过不使用字符串以类型安全的方式获取名称.所以我试图用表达式提取它.
假设我想在此界面中获取方法的名称:
public interface IMyInteface
{
void DoSomething(string param1, string param2);
}
Run Code Online (Sandbox Code Playgroud)
目前我可以使用THIS方法获取名称:
MemberInfo GetMethodInfo<T>(Expression<Action<T>> expression)
{
return ((MethodCallExpression)expression.Body).Method;
}
Run Code Online (Sandbox Code Playgroud)
我可以调用helper方法如下:
var methodInfo = GetMethodInfo<IMyInteface>(x => x.DoSomething(null, null));
Console.WriteLine(methodInfo.Name);
Run Code Online (Sandbox Code Playgroud)
但我正在寻找我可以获取方法名称而不指定参数的版本(null,null)
像这样:
var methodInfo = GetMethodInfo<IMyInteface>(x => x.DoSomething);
Run Code Online (Sandbox Code Playgroud)
但所有尝试都无法编译
有没有办法做到这一点?