Lambda表达式< T,Func键<锡,兜售>>和MethodInfo的 [英] Lambda expressions <T, Func<TIN,TOUT>> and MethodInfo

查看:198
本文介绍了Lambda表达式< T,Func键<锡,兜售>>和MethodInfo的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然从VS2010迁移项目,VS2012,我遇到了以下问题。该项目使用反射了很多,为了从接口下面的代码放在获取MethodInfo的:

 表达式来; FUNC< ITEST,FUNC< ServiceRequest,ServiceResponse>>>表达式= SCV => scv.Get; 

UnaryExpression unaryExpression = expression.Body为UnaryExpression;

MethodCallExpression methodCallExpression = unaryExpression.Operand为MethodCallExpression;

常量表达式常量表达式= methodCallExpression.Arguments [2]如常量表达式;

MethodInfo的myMethod的= constantExpression.Value为MethodInfo的;

这工作得很好用VS2010编译,但methodCallExpression.Arguments.Count()为2,如果代码编译与VS2012针对.NET 4.0。



反编译后,我注意到,编译器为同一个表达式生成不同的代码。



这是一个设计问题,因为设计不应该在幻数接力就像methodCallExpression.Arguments数2 [2]。我试图找到一个解决方案使用下列内容:

  MethodCallExpression outermostExpression =表达。体作为MethodCallExpression; 
MethodInfo的myMethod的= outermostExpression.Method;



但outermostExpression为null。



最后,我做了工作,改变表达如下:

 表达式来; Func键< ITEST,ServiceResponse>>表达式= SCV => scv.Get(默认值(ServiceRequest)); 
MethodCallExpression outermostExpression = expression.Body为MethodCallExpression;
Assert.AreEqual(GET,outermostExpression.Method.Name);



这是不理想的,但它工作在两个VS2010和VS2012。



有没有办法从像下面的表达式找到MethodInfo的:

 表达式来;函数功能:LT ; ITEST,ServiceResponse>>表达式= SCV => scv.Get(默认值(ServiceRequest)); 
的MethodInfo MethodInfo的= GetInnerMethodInfo(表达);
Assert.AreEqual(GET,methodInfo.Name);


解决方案

我不知道为什么有一个在差异这样的表达编译。但是,如果你正在寻找在不断的委托方法的方法的信息,您可以编译与 ITEST 实施中的表达得到在代表 MethodInfo的。例如:

 表达式来; Func键< ITEST,Func键< ServiceRequest,ServiceResponse>>>表达式= SCV =>新的函数功能:LT; ServiceRequest,ServiceResponse>(scv.Get); 
Func键< ServiceRequest,ServiceResponse> RET = expression.Compile()(新测试());
的MethodInfo MethodInfo的= ret.Method;

..其中测试一些类工具 ITEST 。它工作在2012年和2010



我不确定如何可以得到从2012年的表达式的方法没有的信息先编译它...



更新:



如果编译表达式是不是一种选择,看来编译器产生不同的表达,并把在的MethodInfo 在C#编译器5的 MethodCallExpression.Object 属性。您可以检查,看看是否属性不是并使用其价值为的MethodInfo ,或继续得到一个元素在参数集合。例如:

 表达式来; Func键< ITEST,Func键< ServiceRequest,ServiceResponse>>>表达式= 
SCV =>新的函数功能:LT; ServiceRequest,ServiceResponse>(scv.Get);

UnaryExpression unaryExpression = expression.Body为UnaryExpression;

MethodCallExpression methodCallExpression =
unaryExpression.Operand为MethodCallExpression;

常量表达式常量表达式=
methodCallExpression.Object为常量表达式;

的MethodInfo MethodInfo的;
如果(常量表达式!= NULL)
{
MethodInfo的= constantExpression.Value为MethodInfo的;
}
,否则
{
常量表达式= methodCallExpression.Arguments
。单(A => a.Type == typeof运算(MethodInfo的)
和; &安培; a.NodeType == ExpressionType.Constant)为
常量表达式;
MethodInfo的= constantExpression.Value为MethodInfo的;
}



我使用LINQ查询以获得在该元素参数集合,如果你喜欢硬编码的指数,你很可能使用的替代。更完整的错误检查是必要的,也是。


While migrating a project from VS2010 to VS2012, I ran into the following problem. The project is using Reflection a lot, and in order to get the MethodInfo from an interface the following code was placed:

Expression<Func<ITest, Func<ServiceRequest, ServiceResponse>>> expression = scv => scv.Get;

UnaryExpression unaryExpression = expression.Body as UnaryExpression;

MethodCallExpression methodCallExpression = unaryExpression.Operand as MethodCallExpression;

ConstantExpression constantExpression = methodCallExpression.Arguments[2] as ConstantExpression;

MethodInfo myMethod = constantExpression.Value as MethodInfo;

This worked fine compiled with VS2010, but the methodCallExpression.Arguments.Count() was 2 if the code is compiled with VS2012 aiming .Net 4.0.

After decompiling, I noticed that the compiler was generating different code for the same expression.

This is a design problem because the design should not relay on "magic numbers" like the number 2 on methodCallExpression.Arguments[2]. I tried to find a solution for this using the following:

MethodCallExpression outermostExpression = expression .Body as MethodCallExpression;
MethodInfo myMethod = outermostExpression.Method;

But outermostExpression is null.

Finally, I made it work changing the expression as follow:

Expression<Func<ITest, ServiceResponse>> expression = scv => scv.Get(default(ServiceRequest));
MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;
Assert.AreEqual("Get", outermostExpression.Method.Name);

It is not ideal, but it works on both VS2010 and VS2012.

Is there a way to find the MethodInfo from an expression like the following:

Expression<Func<ITest, ServiceResponse>> expression = scv => scv.Get(default(ServiceRequest));
MethodInfo methodInfo = GetInnerMethodInfo( expression );
Assert.AreEqual("Get", methodInfo.Name);

解决方案

I'm not sure why there's a difference in the way the expressions compile. But, if you're looking for the method info of the method in the constant delegate, you can compile the expression with an ITest implementation to get at the delegates MethodInfo. For example:

Expression<Func<ITest, Func<ServiceRequest, ServiceResponse>>> expression = scv => new Func<ServiceRequest, ServiceResponse>(scv.Get);
Func<ServiceRequest, ServiceResponse> ret = expression.Compile()(new Test());
MethodInfo methodInfo = ret.Method;

..where Test is some class and implements ITest. Which works in both 2012 and 2010.

I'm unsure how you can get that method info from the expression in 2012 without first compiling it...

UPDATE:

If compiling the expression isn't an option, it appears that the compiler is generating a different expression and putting the MethodInfo in the MethodCallExpression.Object property in the C# 5 compiler. You can check to see if that property is not null and use its value for the MethodInfo, or continue on getting an element in the Arguments collection. For example:

Expression<Func<ITest, Func<ServiceRequest, ServiceResponse>>> expression = 
    scv => new Func<ServiceRequest, ServiceResponse>(scv.Get);

UnaryExpression unaryExpression = expression.Body as UnaryExpression;

MethodCallExpression methodCallExpression = 
    unaryExpression.Operand as MethodCallExpression;

ConstantExpression constantExpression = 
    methodCallExpression.Object as ConstantExpression;

MethodInfo methodInfo;
if (constantExpression != null)
{
    methodInfo = constantExpression.Value as MethodInfo;
}
else
{
    constantExpression = methodCallExpression.Arguments
                            .Single(a => a.Type == typeof(MethodInfo) 
                                && a.NodeType == ExpressionType.Constant) as
                            ConstantExpression;
    methodInfo = constantExpression.Value as MethodInfo;
}

I'm using the LINQ query to get at the element in the Arguments collection, if you prefer the hard-coded index, you could probably use that instead. More complete error-checking is necessary as well.

这篇关于Lambda表达式&LT; T,Func键&LT;锡,兜售&GT;&GT;和MethodInfo的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆