c# - Expression.Call "Contains"方法抛出 "Ambiguous match found exception"

标签 c# reflection .net-core

在 .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/

相关文章:

java - java中使用反射从类绝对路径获取类属性

c# - 表达式树 InvalidOperationException : The LINQ expression can't be resolved

c# - 从 WCF 中获取一个大的 List<T> block ?

c# - 是否可以在.NET Core Web应用程序的任务管理器中设置显示名称?

c# - 使用C#远程创建数据库

asp.net-mvc - Dotnet Cli - 发布命令不更改 Web 配置中的环境变量

c# - 如何将连接字符串从startup.cs asp.net core传递到UnitOfWork项目

c# - 在 visual studio 2010 中调试时如何计算 c# 中的方法执行时间?

C# 将类型转换为相同类型的 IEnumerable?

java - 当传递枚举的字符串值时,如何在运行时访问任何已知枚举的值?