在 .net 核心 2.1 中
这是我的代码
//call class Expression 's method
public static MethodCallExpression Call(Expression instance, MethodInfo method, params Expression[] arguments);
//service code
var parameter = Expression.Parameter(typeof(T), "x")
var property = Expression.Property(parameter, "Name");
var value = Expression.Constant("xx");
var converted = Expression.Convert(value, property.Type);
var exp = Expression.Call(property, property.Type.GetMethod("Contains"), converted);
//then will throw Ambiguous match found exception
我发现 .net core 2.1 有 4 个方法,.net framework 有 1 个方法,我如何在 .net core 2.1 中修复,在 .net framework 中运行没问题
.net 核心中的方法 /image/vNcks.png
.net 框架中的方法 /image/nTvEp.png
最佳答案
正如您已经注意到的,有多个方法与名称 Contains
相匹配。您应该使用以下 overload `GetMethod() 的定义,它允许您指定方法的类型参数。
var exp = Expression.Call(property, property.Type.GetMethod("Contains",/*Here you need the type parameters*/), converted);
关于c# - Expression.Call "Contains"方法抛出 "Ambiguous match found exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52436165/