我有一个具有以下结构的表达式。我把它从Expression.ArrayIndex
,所以我不能改变它:将IEnumerable强制转换为表达式树中的数组Func
Expression<Func<TModel[], TProperty>> expression
后来我结合使用具有这种结构的父母一个这样表达:
Expression<Func<TDataSource, IEnumerable<TModel>>> dataSourceExpression
因此产生的表述应该是:
Expression<Func<TDataSource, TProperty>>
注意,合并后他们我使用TDataSource
作为输入,我得到TProperty
作为结果。 但是我无法将它们合并,因为TModel[]
无法投射到IEnumerable<TModel>
。
的结合像这样做:
Expression<Func<TDataSource, TProperty>> outputWithoutInline = dataSourceExpression.Combine(expression, true);
public static class ExpressionUtils
{
public static Expression<Func<T1, T3>> Combine<T1, T2, T3>(
this Expression<Func<T1, T2>> outer, Expression<Func<T2, T3>> inner, bool inline)
{
var invoke = Expression.Invoke(inner, outer.Body);
Expression body = inline ? new ExpressionRewriter().AutoInline(invoke) : invoke;
return Expression.Lambda<Func<T1, T3>>(body, outer.Parameters);
}
}
*更多信息有关ExpressionRewriter
:https://stackoverflow.com/a/1720642/1587864
我想用IEnumerable
继续,我真的需要在其他表达的Array
。有什么办法可以在合并之前以某种方式对这些表达式进行转换?
谢谢
你是什么意思将它们组合?你能展示更多的代码吗? – goric