8

我试图弄清楚如何创建一个调用具有引用参数的方法的表达式。

让我用一个简单(但人为)的例子来解释我的问题。考虑方法:

    public static int TwiceTheInput(int x)
    {
        return x*2;
    }

我可以通过执行以下操作创建一个表达式来调用上述方法:

    {
        var inputVar = Expression.Variable(typeof (int), "input");
        var blockExp =
            Expression.Block(
                    new[] {inputVar}
                    , Expression.Assign(inputVar, Expression.Constant(10))
                    , Expression.Assign(inputVar, Expression.Call(GetType().GetMethod("TwiceTheInput", new[] { typeof(int) }), inputVar))
                    , inputVar
                    );
        var result = Expression.Lambda<Func<int>>(blockExp).Compile()();
    }

在执行时,上面的“结果”应该以 20 结束。现在考虑一个使用引用参数的 TwiceTheInput() 版本:

    public static void TwiceTheInputByRef(ref int x)
    {
        x = x * 2;
    }

如何编写类似的表达式树来调用 TwiceTheInputByRef() 并通过引用传递参数?

解决方案:(感谢 Cicada)。利用:

Type.MakeByRefType()

这是生成表达式树的代码段:

        {
        var inputVar = Expression.Variable(typeof(int), "input");
        var blockExp =
            Expression.Block(
                    new[] { inputVar }
                    , Expression.Assign(inputVar, Expression.Constant(10))
                    , Expression.Call(GetType().GetMethod("TwiceTheInputByRef", new[] { typeof(int).MakeByRefType() }), inputVar)
                    , inputVar
                    );
        var result = Expression.Lambda<Func<int>>(blockExp).Compile()();
    }
4

1 回答 1

8

您不必进行太多更改,只需删除Assign并更改typeof(int)typeof(int).MakeByRefType().

var blockExp = Expression.Block(
    new[] { inputVar }
    , Expression.Assign(inputVar, Expression.Constant(10))
    , Expression.Call(
       typeof(Program).GetMethod( 
           "TwiceTheInputByRef", new [] { typeof(int).MakeByRefType() }),
       inputVar)
    , inputVar
);
于 2013-02-18T16:00:04.063 回答