当参数仅在泛型类型中不同时,通过反射获得正确的方法

时间:2017-08-14 16:04:53

标签: c# generics reflection

System.Linq.Queryable有两种Where方法略有不同:

public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate);
public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate);

我需要使用Expression<Func<TSource, bool>>参数选择秒数。

我到目前为止尝试过:

typeof(Queryable).GetTypeInfo()
.GetMethod(nameof(Queryable.Where), new [] { typeof(IQueryable<object>), typeof(Expression<Func<object,bool>>) })
// results in null

typeof(Queryable).GetTypeInfo()
.GetMethod(nameof(Queryable.Where), new [] { typeof(IQueryable<>), typeof(Expression<Func<,>>) })
// Error: Unexpected use of an unbound generic name

typeof(Queryable).GetTypeInfo()
.GetMethod(nameof(Queryable.Where), new [] { typeof(IQueryable<>), typeof(Expression<Func<,>>) })
// Exception: Ambiguous match found

如果不使用GetMethods()并依赖第二种方法作为第二种方法,我必须指定什么来获取第二种方法。

0 个答案:

没有答案