lundi 14 août 2017

Get correct method through reflection when parameters only differ in generic type [duplicate]

This question already has an answer here:

System.Linq.Queryable has 2 Where Methods that differ sligtly:

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);

I need to select the seconds one with the Expression<Func<TSource, bool>> parameter.

I tried so far:

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

What do I have to specify to get the second method without using GetMethods() and relying on it being the second one.





Aucun commentaire:

Enregistrer un commentaire