c#-4.0 - 如何获取重载静态方法的 MethodInfo?

标签 c#-4.0 generics reflection

当我运行时:

MethodInfo m = typeof(Expression).GetMethod("Lambda", new Type[]{typeof(Expression), typeof(ParameterExpression[])});

我得到:

System.Reflection.AmbiguousMatchException: Ambiguous match found.

这是为了通用地做到这一点(而不是知道它是一个字符串)

var newExpression = Expression.Lambda<Func<T, string>>(propertyExpression, parameters);
return entities.OrderBy(newExpression);

编辑:附加信息:

运行:

typeof(Expression).GetType().GetMethod("GetMethodCandidates", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(typeof(Expression),new object[]{"Lambda", BindingFlags.Static| BindingFlags.Public, CallingConventions.Standard,new Type[]{typeof(Expression), typeof(ParameterExpression).MakeArrayType()},false})

返回数组:

[0]: {System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])}
[1]: {System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression, System.Collections.Generic.IEnumerable`1[System.Linq.Expressions.ParameterExpression])}
[2]: {System.Linq.Expressions.LambdaExpression Lambda(System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])}
[3]: {System.Linq.Expressions.LambdaExpression Lambda(System.Linq.Expressions.Expression, System.Collections.Generic.IEnumerable`1[System.Linq.Expressions.ParameterExpression])}

最佳答案

不幸的是,反射是在泛型可用之前编写的,并且通常根本不能很好地支持泛型方法/类。

有人提出了与此类似的问题(GetMethod for generic method),答案是获取候选方法并从方法集合中选择您想要的方法。

关于c#-4.0 - 如何获取重载静态方法的 MethodInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11892760/

相关文章:

c#-4.0 - MongoDb 中的继承 : how to request instances of defined type

performance - 渐近行为 IEnumerable.Intersect 与 HashedSet.IntersectWith

c# - 从处理抽象类实例的抽象类<T>调用方法

c# - 是否可以让非泛型方法返回泛型类型?

c# - 如何获取基类的所有继承类?

c# - 隐式转换为 IEnumerable<T>

linq - 如何使用 Linq.Expressions 查询集合

swift - var 符合具有 associateType 的协议(protocol)

c# - 如何在 C# 中使用反射检查集合类型属性的相等性

java - JPA:在实体层次结构中搜索关联实体