简体   繁体   English

无法在表达式之间转换 <Func<…> &gt;和Func &lt;…&gt;

[英]Cannot Convert between Expression<Func<…>> and Func<…>

A method I am trying to call (third party, can't change) expects a Func<TResult> . 我尝试调用的方法(第三方,不能更改)期望Func<TResult> Using reflection I am trying to make an eg Func<Task<IEnumerable<Person>>> but I am inadvertently creating an Expression<Func<Task<IEnumerable<Person>>>> . 使用反射,我试图制作一个Func<Task<IEnumerable<Person>>>但是我无意中创建了一个Expression<Func<Task<IEnumerable<Person>>>> The reason for reflection is that person is arbitrary and could be any type depending on how/when this is called. 反思的原因是,人是任意的,并且可以根据调用方式/时间的不同而定。

// 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] });

When I use lambda later I get the exception 当我稍后使用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]]]'.

Is there a better way than Expression.Lambda to build a Func<> on the fly? 有没有比Expression.Lambda更好的方法来动态构建Func<> I can't seem to find the correct way to do this. 我似乎找不到正确的方法来执行此操作。

Thank you 谢谢

You're looking for the .Compile() method, which does exactly that. 您正在寻找.Compile()方法,该方法正是这样做的。

Side note: 边注:
You don't need to call the Expression APIs using Reflection; 您无需使用Reflection调用Expression API; you can use the non-generic Expression.Lambda() , which will still return a Func<T> : 您可以使用非通用的Expression.Lambda() ,但仍将返回Func<T>

Expression.Lambda(body).Compile()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从Expression &lt;Func &lt;实体,bool&gt;&gt;转换为Func &lt;实体,bool&gt; - Cannot convert from Expression< Func< Entity, bool> > to Func< Entity, bool> Expression和Func之间的区别 - difference between Expression and Func 无法转换类型&#39;Expression <System.Func<TP,Action> &gt;”到“表达式 <System.Func<TP,BasePageElement> &gt;&#39; - Cannot convert type 'Expression<System.Func<TP,Action>>' to 'Expression<System.Func<TP,BasePageElement>>' 无法从表达式转换 <Func<T1, T2> &gt;`到`Expression <Func<object, object> &gt;` - Cannot convert from `Expression<Func<T1, T2>>` to `Expression<Func<object, object>>` 无法从表达式转换<func<t,bool> &gt; 到表达式<func<user,bool> &gt; 当 T = class 内的用户时</func<user,bool></func<t,bool> - Cannot convert from Expression<Func<T,bool>> to Expression<Func<User,bool>> when T = User within class 无法将异步Lambda表达式转换为委托类型&#39;Func <UIAccessibilityCustomAction, bool> - Cannot convert async lambda expression to delegate type 'Func<UIAccessibilityCustomAction, bool> 转换表达式 <Func<TEntity,TKey> 表达 <Func<TEntity, Object> &gt; - Convert Expression<Func<TEntity,TKey> to Expression<Func<TEntity, Object>> 转换表达式 <Func<XClass, object> &gt;表达 <Func<YClass, object> &gt; - Convert Expression<Func<XClass, object>> to Expression<Func<YClass, object>> 转换表达式 <Func<TDocument, object> &gt;表达 <Func<TDocument, TOutput> &gt; - Convert Expression<Func<TDocument, object>> to Expression<Func<TDocument, TOutput>> 转换表达式 <Func<T, object> &gt;表达 <Func<object> &gt; - Convert Expression<Func<T, object>> to Expression<Func<object>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM