我整天都在阅读有关这方面的帖子,但似乎没有一个与我所掌握的情况相匹配。我有一个具有以下方法的类:
IQueryable<TBusinessContract> Query<TBusinessContract>(
Expression<Func<TBusinessContract, bool>> condition, params string[] children )
where TBusinessContract : BusinessContract;
IQueryable<TSubType> Query<TSuperType, TSubType>(
Expression<Func<TSubType, bool>> condition, params string[] children )
where TSuperType : BusinessContract
where TSubType : BusinessContract;
我想为第一个获取 MethodInfo。我尝试了许多不同的组合和排列,我得到 null 或模棱两可的匹配异常。我想出了以下可行的方法,但感觉有点笨拙。
MethodInfo queryMethod = Dal.GetType()
.GetMethods( BindingFlags.Public | BindingFlags.Instance )
.Where( mi => mi.Name == "Query" )
.Where( mi => mi.IsGenericMethod )
.Where( mi => mi.GetGenericArguments().Length == 1 )
.SingleOrDefault();
这是我能做的最好的还是我错过了什么?我正在使用.NET 4.5。