问题描述
我们必须键入 T1
有类型的成员 T
的实体。
是这样的:
公共类T1
{
公共t。成员{搞定;设置;}
}
用户可以使用我们的UI给我们在T A过滤器和我们把它翻译成该得到一个T和回报布尔函数(表达式来; Func键< T,BOOL>>)的表达
我想知道是否有可能将其转换为,获取T1和返回布尔函数的表达式。
其实我倒喜欢这种转换:
(T => t.Member1 == someValue中和放大器;&安培; t.Member2 == someOtherValue );
这样:
(T1 => t1.Member.Member1 == someValue中和放大器;&安培; t1.Member.Member2 == someOtherValue);
您可以用一些方式做到这一点。
首先,最简单的:使用的
表达式来; Func键< T,BOOL>> exprT = t.Member1 == someValue中和放大器;&安培; t.Member2 == someOtherValue
ParameterExpression P = Expression.Parameter(typeof运算(T1));
VAR EXPR = Expression.Invoke(表达式,Expression.PropertyOrField(P,会员));
表达式来; Func键< T1,布尔>> exprT1 = Expression.Lambda<&Func键LT; T1,布尔>>(表达式,P);
但在这种情况下,你不能
T1 => (T =>(t.Member1 == someValue中和放大器;&安培; t.Member2 == someOtherValue))(t1.Member),
而不是
(T1 => t1.Member.Member1 == someValue中和放大器; &安培; t1.Member.Member2 == someOtherValue);
有关更换可以使用的像
V类:ExpressionVisitor
{
公共ParameterExpression参数{搞定;私人集; }
表达米;
公共V(类型参数类型,字符串成员)
{
参数= Expression.Parameter(参数类型);
this.m = Expression.PropertyOrField(参数,成员);
}
保护覆盖表达VisitParameter(ParameterExpression节点)
{
如果(node.Type == m.Type)
{
返回米;
}
返回base.VisitParameter(节点);
}
}
和使用
变种v =新的v(typeof运算(T1),会员);
VAR exprT1 = Expression.Lambda<&Func键LT; T1,布尔>>(v.Visit(exprT.Body),v.Parameter);
We have an entity of type T1
which has a member of type T
.something like this :
public class T1
{
public T Member{get;set;}
}
User can use our UI to give us a filter over T and we have translate it to an expression of a function that gets a T and returns bool (Expression<Func<T,bool>>)
I would like to know is it possible to convert this to an expression of a function that gets T1 and returns bool.
Actually I'd like to convert this :
(t=>t.Member1==someValue && t.Member2==someOtherValue);
to this :
(t1=>t1.Member.Member1==someValue && t1.Member.Member2==someOtherValue);
You can do it with a few way.
First and simplest: use Expression.Invoke
Expression<Func<T, bool>> exprT = t.Member1==someValue && t.Member2==someOtherValue
ParameterExpression p = Expression.Parameter(typeof(T1));
var expr = Expression.Invoke(expr, Expression.PropertyOrField(p, "Member"));
Expression<Func<T1, bool>> exprT1 = Expression.Lambda<Func<T1, bool>>(expr, p);
but in this case you get not
t1 => (t=>(t.Member1==someValue && t.Member2==someOtherValue))(t1.Member),
instead of
(t1=>t1.Member.Member1==someValue && t1.Member.Member2==someOtherValue);
For replacing you can use ExpressionVisitor class like
class V : ExpressionVisitor
{
public ParameterExpression Parameter { get; private set; }
Expression m;
public V(Type parameterType, string member)
{
Parameter = Expression.Parameter(parameterType);
this.m = Expression.PropertyOrField(Parameter, member);
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (node.Type == m.Type)
{
return m;
}
return base.VisitParameter(node);
}
}
and use it
var v = new V(typeof(T1), "Member");
var exprT1 = Expression.Lambda<Func<T1, bool>>(v.Visit(exprT.Body), v.Parameter);
这篇关于转换一个表达式来; Func键< T,BOOL>>一个表达式来; Func键< T1,布尔>>使T是T1的一个部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!