我如何打电话与前pression树木引用变量的方法变量、树木、方法、pression

2023-09-04 13:28:09 作者:傻啦吧唧的

我试图找出如何创建一个防爆pression调用它有一个基准参数的方法。

让我解释一下我的问题有一个简单(但人造的)例子。考虑方法:

 公共静态INT TwiceTheInput(INT X)
    {
        返回X * 2;
    }
 

我可以创建一个防爆pression通过执行类似调用上述方法:

  {
        VAR inputVar =前pression.Variable(typeof运算(INT),输入);
        VAR blockExp =
            防爆pression.Block(
                    新的[] {} inputVar
                    ,防爆pression.Assign(inputVar,防爆pression.Constant(10))
                    ,防爆pression.Assign(inputVar,防爆pression.Call(的GetType()。GetMethod的(TwiceTheInput,新的[] {typeof运算(INT)}),inputVar))
                    ,inputVar
                    );
        VAR的结果=前pression.Lambda< Func键< INT>>(blockExp).Compile()();
    }
 
4.1 基本类型和引用类型的值

在执行中,结果上方应该结束了值20。 现在考虑的一个版本TwiceTheInput(),其使用由引用参数:

 公共静态无效TwiceTheInputByRef(REF INT X)
    {
        X = X * 2;
    }
 

我如何写一个类似的前pression树调用TwiceTheInputByRef()和按引用传递参数呢?

解决方法:(感谢蝉)。用途:

  Type.MakeByRefType()
 

下面是一个code段生成前pression树:

  {
        VAR inputVar =前pression.Variable(typeof运算(INT),输入);
        VAR blockExp =
            防爆pression.Block(
                    新的[] {} inputVar
                    ,防爆pression.Assign(inputVar,防爆pression.Constant(10))
                    ,防爆pression.Call(的GetType()。GetMethod的(TwiceTheInputByRef,新的[] {typeof运算(INT).MakeByRefType()}),inputVar)
                    ,inputVar
                    );
        VAR的结果=前pression.Lambda< Func键< INT>>(blockExp).Compile()();
    }
 

解决方案

您不必太大变化,只是删除分配和变化 typeof运算(INT) typeof运算(INT).MakeByRefType()

  VAR blockExp =前pression.Block(
    新的[] {} inputVar
    ,防爆pression.Assign(inputVar,防爆pression.Constant(10))
    ,防爆pression.Call(
       typeof运算(程序).GetMethod(
           TwiceTheInputByRef,新的[] {typeof运算(INT).MakeByRefType()}),
       inputVar)
    ,inputVar
);
 

I am trying to figure out how to create an Expression that calls a method which has a reference parameter.

Let me explain my question with a simple (but artificial) example. Consider the method:

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

I can create an Expression to call the above method by doing something like:

    {
        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()();
    }

On execution, the "result" above should end up with a value of 20. Now consider a version of TwiceTheInput() that uses by-reference parameters:

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

How do I write a similar Expression Tree to call TwiceTheInputByRef() and pass arguments by reference to it?

Solution: (Thanks to Cicada). Use:

Type.MakeByRefType()

Here's a code segment to generate the Expression Tree:

        {
        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()();
    }

解决方案

You don't have to change much, just remove the Assign and change typeof(int) to 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
);