构建表达式树会抛出NotSupportedException"不支持TryExpression作为方法的参数。"

时间:2013-10-28 00:03:27

标签: c# lambda expression-trees

我正在尝试构建表达式树,但在编译时会抛出NotSupportedException:

  

不支持TryExpression作为方法'Boolean TryGetMemberValue(System.Object,System.String,System.Object ByRef)'的参数,因为它有一个带有by-ref类型的参数。构造树,使TryExpression不嵌套在此表达式中。

我有点理解它的内容,但我无法弄清楚我需要改变什么......这是代码:

static void Main()
{
    //A.Child.Name
    var child = CreateExpression("Child", proxyParameter);
    var name = CreateExpression("Name", child);

    var expr = Expression.Lambda<Func<Proxy, object>>(name, proxyParameter);

    var func = expr.Compile();
}

class A
{
    public A Child { get; set; }
    public string Name { get; set; }
}

abstract class Proxy
{
    public abstract bool TryGetMemberValue(object parent, string name, out object result);
}

static ParameterExpression proxyParameter = Expression.Parameter(typeof(Proxy), "proxy");

static Expression CreateExpression(string propertyName, Expression parent)
{
    var tryGetMemberMethod = typeof(Proxy).GetMethod("TryGetMemberValue");

    var result = Expression.Variable(typeof(object), "out result");

    var returnTarget = Expression.Label(typeof(object));

    var tryGetMember =
        Expression.Block(
            new[] { result },
            Expression.IfThenElse(Expression.Equal(Expression.Call(proxyParameter, tryGetMemberMethod, parent, Expression.Constant(propertyName), result), Expression.Constant(true)),
                Expression.Return(returnTarget, result),
                Expression.Throw(Expression.Constant(new MissingMemberException(propertyName)))),
            Expression.Label(returnTarget, Expression.Constant(null)));

    return tryGetMember;
}

生成以下表达式:

.Lambda #Lambda1<System.Func`2[Program+Proxy,System.Object]>(Program+Proxy $proxy) {
.Block(System.Object $'out result') {
    .If (.Call $proxy.TryGetMemberValue(
        .Block(System.Object $'out result') {
            .If (.Call $proxy.TryGetMemberValue(
                $proxy,
                "Child",
                $'out result') == True) {
                .Return #Label1 { $'out result' }
            } .Else {
                .Throw .Constant<System.MissingMemberException>(System.MissingMemberException: Child)
            };
            .Label
                null
            .LabelTarget #Label1:
        },
        "Name",
        $'out result') == True) {
        .Return #Label2 { $'out result' }
    } .Else {
        .Throw .Constant<System.MissingMemberException>(System.MissingMemberException: Name)
    };
    .Label
        null
    .LabelTarget #Label2:
}

有什么想法吗?

/ *编辑* / 有趣的是,这很好用:

var tryGetMember =
    Expression.Block(
        new[] { result },
        Expression.Call(proxyParameter, tryGetMemberMethod, parent, Expression.Constant(propertyName), result),
    result);

1 个答案:

答案 0 :(得分:2)

虽然异常消息还远未明确,但我认为您刚刚遇到Expression编译器的限制:在调用表达式下,您不能在具有ref参数的方法中使用复杂的子表达式。

我认为你应该做些什么来解决这个问题与你在C#代码中做的基本相同:使用局部变量来保持中间结果:

var child = Expression.Variable(typeof(object), "child");

var body = Expression.Block(
    new[] { child },
    Expression.Assign(child, CreateExpression("Child", proxyParameter)),
    CreateExpression("Name", child));

var expr = Expression.Lambda<Func<Proxy, object>>(body, proxyParameter);

通过此更改,代码不再引发异常。

此外,您无需在Label表达式中使用ReturntryGetMember。相反,您可以将IfThenElse替换为Condition(基本上是来自C#的三元运算符)。如果这样做,您还需要指定Throw的类型(即使它实际上从未返回):

var tryGetMember =
    Expression.Block(
        new[] { result },
        Expression.Condition(
            Expression.Equal(
                Expression.Call(
                    proxyParameter, tryGetMemberMethod, parent,
                    Expression.Constant(propertyName), result),
                Expression.Constant(true)),
            result,
            Expression.Throw(
                Expression.Constant(new MissingMemberException(propertyName)),
                typeof(object))));