Expression.Call with Any method throws exception
Asked Answered
S

1

8

I am working on filter mechanism using expressions and can't figure out how call Any method using Expression.Call. Below is example which doesn't make sense but illustrates my problem :

var person = new List<String>(new[] { "Peter", "John", "Jim" });
var personQuery = person.AsQueryable();

var anyMethod = typeof(Queryable).GetMethods().FirstOrDefault(method => method.Name == "Any" && method.GetParameters().Count() == 2);

Expression<Func<String, bool>> expr = p => p == "Amy";

// person.Any(person => person == "Amy"
var call = Expression.Call(
    anyMethod,
    personQuery.Expression,
    expr
);

Expression.Call throws ArgumentException:

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Method Boolean Any[TSource](System.Linq.IQueryable`1[TSource], System.Linq.Expressions.Expression`1[System.Func`2[TSource,System.Boolean]]) is a generic method definition.
  Source=System.Core
  StackTrace:
       w System.Linq.Expressions.Expression.ValidateMethodInfo(MethodInfo method)
       w System.Linq.Expressions.Expression.ValidateMethodAndGetParameters(Expression instance, MethodInfo method)
       w System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0, Expression arg1)
       w TestConsoleApplication.Program.Main(String[] args) w d:\Users\user\Documents\Visual Studio 2012\Projects\TestConsoleApplication\TestConsoleApplication\Program.cs:wiersz 26
       w System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       w System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       w System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Seychelles answered 15/9, 2014 at 10:4 Comment(2)
Could you translate the exception message text into English for us?Dormeuse
Exception message translated.Seychelles
U
15

Your anyMethod variable will contain generic method definition. I.e Any<TSource> you need to convert it to Any<String> before invoking it.

You'll do it by calling anyMethod.MakeGenericMethod providing typeof(String) argument. So your code becomes

var person = new List<String>(new[] { "Peter", "John", "Jim" });
var personQuery = person.AsQueryable();

var anyMethod = typeof(Queryable).GetMethods().FirstOrDefault(method => method.Name == "Any" && method.GetParameters().Count() == 2);
var specificMethod = anyMethod.MakeGenericMethod(typeof(String));//<--Important
Expression<Func<String, bool>> expr = p => p == "Amy";

// person.Any(person => person == "Amy"
var call = Expression.Call(
    specificMethod,
    personQuery.Expression,
    expr
);
Uncertainty answered 15/9, 2014 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.