- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在将一个项目从VS2010迁移到VS2012时,我遇到了以下问题。该项目大量使用反射,为了从接口(interface)获取 MethodInfo,放置了以下代码:
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;
使用 VS2010 编译时效果很好,但是如果代码是使用针对 .Net 4.0 的 VS2012 编译的,则 methodCallExpression.Arguments.Count() 为 2。
反编译后,我注意到编译器为同一个表达式生成了不同的代码。
这是一个设计问题,因为设计不应依赖于“魔数(Magic Number)”,例如 methodCallExpression.Arguments[2] 上的数字 2。我尝试使用以下方法找到解决方案:
MethodCallExpression outermostExpression = expression .Body as MethodCallExpression;
MethodInfo myMethod = outermostExpression.Method;
但是 outermostExpression 为空。
最后,我按如下方式改变了表达式:
Expression<Func<ITest, ServiceResponse>> expression = scv => scv.Get(default(ServiceRequest));
MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;
Assert.AreEqual("Get", outermostExpression.Method.Name);
它并不理想,但它适用于 VS2010 和 VS2012。
有没有办法从如下表达式中找到 MethodInfo:
Expression<Func<ITest, ServiceResponse>> expression = scv => scv.Get(default(ServiceRequest));
MethodInfo methodInfo = GetInnerMethodInfo( expression );
Assert.AreEqual("Get", methodInfo.Name);
最佳答案
我不确定为什么表达式的编译方式不同。但是,如果您要查找常量委托(delegate)中方法的方法信息,则可以使用 ITest
实现编译表达式以获取委托(delegate) MethodInfo
。例如:
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;
..其中 Test
是某个类并实现了 ITest
。这在 2012 年和 2010 年都有效。
我不确定在 2012 年如何在不首先编译的情况下从表达式中获取该方法信息...
如果编译表达式不是一个选项,编译器似乎正在生成一个不同的表达式并将 MethodInfo
放入 C# 的 MethodCallExpression.Object
属性中5 编译器。您可以检查该属性是否不是 null
并将其值用于 MethodInfo
,或者继续获取 Arguments
集合中的元素.例如:
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;
}
我正在使用 LINQ 查询获取 Arguments
集合中的元素,如果您更喜欢硬编码索引,则可以改用它。还需要更完整的错误检查。
关于c# - Lambda 表达式 <T, Func<TIN,TOUT>> 和 MethodInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12166785/
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前等待bepreEach()块中的两个异步函数
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前,在beforeEach()块中等待两个Ja
为什么是Func<>从 Expression> 创建通过 .Compile() 比仅使用 Func<> 慢得多直接声明? 我刚从使用 Func 更改为直接声明为从 Expression> 创建的一个在
我正在创建一个 Validator类(class)。我正在尝试实现 Linq SelectMany我的验证器的扩展方法能够使用 Linq 查询组合表达式并验证最终结果,即使基础值发生变化也是如此。 下
function sum(a) { let currentSum = a; function f(b) { currentSum += b; return f; }
我只知道i = i++;是未定义的行为,但是如果一个表达式中调用了两个或多个函数,并且所有功能是一样的。是未定义吗?例如: int func(int a) { std::cout << a <
我如何定义一个对象,以便作用于它的任何函数都作用于它的一个字段?这可能吗? class Mydata(object): def __init__(self, val): sel
这个问题一直很有趣,尽管它不一定很整洁。我有以下代码: import random def d(m): return random.randint(1, m) print(3*d(6)) 这将
能否请您解释一下使用 func.apply(null, arr) 的区别?和 func.apply(this, arr)在下面的代码示例中? var Foo = function() { fu
我想收集/运行任务,然后对它们执行 Task.WhenAll。 var tasks = new List(); foreach (var thing in things) { tasks.Add(
我有以下代码: static Func s_objToString = (x) => x.ToString(); static Func s_stringToString = s_objToStrin
相关主题: Create Expression> dynamically 我在互联网上搜索但所有样本都解释了 Expression来自 T ? 谢谢 编辑 1) T输入我的代码在运行时确定,例如我想用
我正在尝试使用 LinqKit 动态生成 linqtosql 查询.在将表达式发送到 LinqKit 之前,我想检查要为预测添加的字段。所以我想出了一些想法,比如 Expression> GetPr
我遇到了一些麻烦,我写了一个 Func,IDE 不喜欢我在 Func 体内调用 Func ,我不太明白为什么,因为如果我将这个确切的代码放在方法体中,并使用相同的返回类型和参数,那么它就可以工作。 代
我现在正在学习使用 Class 语法来创建 React 组件,请注意我现在必须声明这样的方法: class Foo extends React.Component { ... bar
下面两种说法有区别吗?他们都工作。 if ( ((Func)(()=>true))() ) { .... }; if ( new Func(()=>true)()) { .... }; 最佳答案 不,
这个问题在这里已经有了答案: Difference between func() and (*this).func() in C++ (4 个答案) 关闭 6 年前。 如果我有一个带有虚函数而没有自
主要问题是“是否可以将任何类型的 func 作为参数传递以及如何传递?”。我正在学习 Go 并且想像这样制作我自己的异步包装函数: func AsyncFunc(fn func(), args ...
有没有简单的转换方法 Expression> 到 Expression> T从哪里继承自TBase? 最佳答案 只要 T 派生自 TBase,您就可以使用原始表达式的主体和参数直接创建所需类型的表达式
我有以下方法,其中 T 在 Func 中使用: public void DoSomething(string someString, Func someMethod) { if(some
我是一名优秀的程序员,十分优秀!