1

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.

wertzui
  • 5,148
  • 3
  • 31
  • 51
  • Does this part compile at all? `typeof(Expression>)` Here this gives me "Unexpected use of an unbound generic name". – Lasse V. Karlsen Aug 14 '17 at 16:10
  • If you can construct `Type` objects for the parameters then you can use `GetMethod` but I doubt you will be able to construct them using `TSource` as part of the `Type`. – Lasse V. Karlsen Aug 14 '17 at 16:11
  • Note that you cannot use open generic arguments like this, you have to close them even if you close them with the type parameter, this method `public void Method(List list) { }` cannot be found by this code: `typeof(Test).GetTypeInfo().GetMethod(nameof(Test.Method), new[] { typeof(List<>) })`, you have to have the `T` in there. – Lasse V. Karlsen Aug 14 '17 at 16:12
  • You can verify my second comment by actually using `.GetMethods()`, grabbing the right method, extracting the `ParameterType` of its parameters, then passing them back through a call to `.GetMethod(...)`. This will return the right method back to you. However, I don't think you can easily construct those `Type` values in the first place. – Lasse V. Karlsen Aug 14 '17 at 16:15
  • I personally find solving such task much easier by using prototype expressions. But since you seem to ask for reflection only solution, the question is a dupe. – Ivan Stoev Aug 14 '17 at 16:32

0 Answers0