How do I compile an Expression Tree into a callable method, C#?

后端 未结 2 1740
青春惊慌失措
青春惊慌失措 2020-12-09 05:23

I have an expression tree I have created by parsing an Xml using the expression class in C#. See this question.

I only have Add, Subtract, Divide, Multiply, Paramete

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 06:11

    You need to create a lambda - i.e.

    var lambda = Expression.Lambda>(body, param);
    Func method = lambda.Compile();
    int v = method(1.0); // test
    

    where "body" is your expression tree (taking a float, returning an int) involving the ParameterExpression param.

    You might also find this and this helpful.

提交回复
热议问题