问题描述
我知道有这个网站上的几个答案,我道歉,如果这是任何方式复制,但我发现的那些的没有做什么,我试图做的。
我想指定的方法的信息,所以我可以不使用字符串获得一个类型安全方式的名称。
所以,我想和前pression来解压。
说我想在这个接口中的方法的名称:
公共接口IMyInteface
{
无效DoSomething的(字符串参数1,字符串参数2);
}
目前我可以用这个方法得到的名称:
的MemberInfo GetMethodInfo< T>(防爆pression<作用< T>>前pression)
{
返回((MethodCallEx pression)前pression.Body)。方法;
}
我可以调用辅助方法如下:
VAR的MethodInfo = GetMethodInfo< IMyInteface>(X => x.DoSomething(NULL,NULL));
Console.WriteLine(methodInfo.Name);
但我要找的版本,我可以得到的方法名称不指定参数(NULL,NULL)
是这样的:
VAR的MethodInfo = GetMethodInfo< IMyInteface>(X => x.DoSomething);
但所有尝试都失败编译
有没有办法做到这一点?
X => x.DoSomething
为了使这个编译我看到的只有两种方式:
- 转至非通用的方式,并指定它的参数为
动作<字符串,字符串>
- 指定
动作<字符串,字符串>
作为自己的目标委托类型:GetMethodInfo< IMyInteface>(X =>新建动作<字符串,串>(x.DoSomething))
如果你确定去与第二个,它可以让你忽略的参数,那么你可以写你的 GetMethodInfo
方法如下:
的MemberInfo GetMethodInfo< T>(防爆pression<&Func键LT; T,代表>>前pression)
{
VAR unaryEx pression =(UnaryEx pression)前pression.Body;
VAR methodCallEx pression =(MethodCallEx pression)unaryEx pression.Operand;
VAR methodInfoEx pression =(ConstantEx pression)methodCallEx pression.Arguments.Last();
VAR MethodInfo的=(的MemberInfo)methodInfoEx pression.Value;
返回MethodInfo的;
}
它适用于你的界面,但可能有些泛化将被要求使这一带的任何方法的工作,这是给你的。
I know there are a few answers on the site on this and i apologize if this is in any way duplicate, but all of the ones I found does not do what I am trying to do.
I am trying to specify method info so I can get the name in a type safe way by not using strings.So I am trying to extract it with an expression.
Say I want to get the name of a method 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 am looking for the version that I can get the method name without specifying the parameters (null, null)
like this:
var methodInfo = GetMethodInfo<IMyInteface>(x => x.DoSomething);
But all attempts fail to compile
Is there a way to do this?
x => x.DoSomething
In order to make this compilable I see only two ways:
- Go non-generic way and specify it's parameter as
Action<string, string>
- Specify
Action<string, string>
as your target delegate type by yourself:GetMethodInfo<IMyInteface>(x => new Action<string,string>(x.DoSomething))
if you are ok to go with second one, which allows you to omit arguments then you can write your GetMethodInfo
method as follows:
MemberInfo GetMethodInfo<T>(Expression<Func<T, Delegate>> expression)
{
var unaryExpression = (UnaryExpression) expression.Body;
var methodCallExpression = (MethodCallExpression) unaryExpression.Operand;
var methodInfoExpression = (ConstantExpression) methodCallExpression.Arguments.Last();
var methodInfo = (MemberInfo) methodInfoExpression.Value;
return methodInfo;
}
It works for your interface, but probably some generalization will be required to make this working with any method, that's up to you.
这篇关于使用前pression获取方法的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!