【发布时间】:2014-04-02 18:01:24
【问题描述】:
我正在尝试使用反射返回正确的“Where”扩展方法,以构建自定义表达式。我尝试了几种方法,但我得到的最接近的方法抛出异常: “在 mscorlib.dll 中发生了 'System.Reflection.AmbiguousMatchException' 类型的未处理异常”
我知道这是因为在 Enumrable 类中定义了两个 Where 方法 - 但是我怎样才能返回只使用谓词的 Where 方法
Func<T, bool>.
我现在拥有的是:
var collectionType = typeof(TSub);
Type tIEnumerable = typeof(IEnumerable<>).MakeGenericType(collectionType);
MethodInfo methodInfo =
typeof(Enumerable)
.GetMethod("Where")
.MakeGenericMethod(collectionType);
我也试过(这个返回null):
MethodInfo methodWhere = typeof(Enumerable).GetMethod("Where", new[] { typeof(TSub )});
and(也返回 null)
MethodInfo methodWhere = typeof(Enumerable).GetMethod("Where", new[] { collectionType })
和(这个返回相同的歧义异常)
MethodInfo methodWhere = typeof(Enumerable).GetMethod("Where", BindingFlags.Public | BindingFlags.Static)
有人可以帮忙吗?
谢谢
【问题讨论】:
标签: c# expression where methodinfo