2

I'm trying to build an expression tree but it throws NotSupportedException when compiled:

TryExpression is not supported as an argument to method 'Boolean TryGetMemberValue(System.Object, System.String, System.Object ByRef)' because it has an argument with by-ref type. Construct the tree so the TryExpression is not nested inside of this expression.

I sort of understand what it says but I can't figure out what I need to change... here's the code:

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;
}

Which generates the following expression:

.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:
}

Any ideas?

/* edited */ interestingly enough this works fine:

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

1 回答 1

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和。相反,您可以替换为(基本上是 C# 中的三元运算符)。如果你这样做,你还需要指定的类型(即使它从未真正返回):ReturntryGetMemberIfThenElseConditionThrow

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))));
于 2013-10-28T13:11:17.760 回答