English 中文(简体)
明确建立植根于任何类型遗产的表达-树木
原标题:Declaratively build expression-tree rooted in a node of any type
  • 时间:2009-09-15 14:06:09
  •  标签:

MSDN说:

The compiler can also build an expression tree for you. A compiler-generated expression tree is always rooted in a node of type Expression<TDelegate>; that is, its root node represents a lambda expression.

但是,如果我想要建立植根于“卫理会”、“金字节”等神树的话? 也不希望以人工方式这样做。

问题回答

工作是宣布2个助手职能

public Expression GetBody(Expression<Action> lambda)
{
    return lambda.Body;
}

public Expression GetBody<TResult>(Expression<Func<TResult>> lambda)
{
    return lambda.Body;
}

使用实例:

var e1 = (MethodCallExpression)GetBody(() => this.FunA());
var e2 = (ConstantExpression)GetBody(() => 4 + 5);
var e3 = (BinaryExpression)GetBody(() => a + b);




相关问题
热门标签