无法在Expression <Func <... >>和Func <...>之间进行转换

Gar*_*son 0 .net c# generics reflection lambda

我试图呼叫的方法(第三方,不能改变)期望a Func<TResult>.使用反射我试图做一个例如,Func<Task<IEnumerable<Person>>>但我无意中创建了一个Expression<Func<Task<IEnumerable<Person>>>>.反思的原因是人是任意的,可以是任何类型,取决于如何/何时调用它.

// trying to mimic this hard-code: () => TaskEx.FromResult(Enumerable.Empty<Person>())
//var enumerableType = typeof(Person); //potentially

var empty = (typeof(Enumerable)).GetMethod("Empty");
var genereicEmpty = empty.MakeGenericMethod(enumerableType);
var emptyEnumerable = genereicEmpty.Invoke(null, null);

var fromResult = typeof (TaskEx).GetMethod("FromResult");
var genericFromResult = fromResult.MakeGenericMethod(genereicEmpty.ReturnType);

var enumerableTask = genericFromResult.Invoke(null, new [] {emptyEnumerable});

var functype = typeof(Func<>).MakeGenericType(genericFromResult.ReturnType);
var body = Expression.Constant(enumerableTask);
var lambdaType = typeof(Expression).GetMethods()
    .Where(x => x.Name == "Lambda")
    .Where(x => x.GetParameters().Length == 2)
    .Where(x => x.GetParameters()[1].ParameterType == typeof(ParameterExpression[]))
    .Single(x => x.IsGenericMethod);
var genericLambdaType = lambdaType.MakeGenericMethod(functype);
var lambda = genericLambdaType.Invoke(null, new object[] { body, new ParameterExpression[0] });
Run Code Online (Sandbox Code Playgroud)

当我稍后使用lambda时,我得到了异常

Exception thrown: 'System.ArgumentException' in mscorlib.dll

Additional information: Object of type 'System.Linq.Expressions.Expression`1[System.Func`1[System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[DC.Dataloader.Models.Person]]]]' cannot be converted to type 'System.Func`1[System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[DC.Dataloader.Models.Person]]]'.
Run Code Online (Sandbox Code Playgroud)

有没有比在飞行中Expression.Lambda建立更好的方法Func<>?我似乎无法找到正确的方法来做到这一点.

谢谢

SLa*_*aks 5

你正在寻找这种.Compile()方法,它正是这样做的.

附注:
您不需要Expression使用Reflection 调用API; 你可以使用非泛型Expression.Lambda(),它仍将返回Func<T>:

Expression.Lambda(body).Compile()
Run Code Online (Sandbox Code Playgroud)