What does Lambda Expression Compile() method do?

前端 未结 3 1055
鱼传尺愫
鱼传尺愫 2020-12-13 09:34

I am trying to understand AST in C#. I wonder, what exactly Compile() method from this example does.

// Some code skipped    
Expression

        
3条回答
  •  有刺的猬
    2020-12-13 09:46

    An Expression represents a data structure in the form of an expression tree - using Compile() this expression tree can be compiled into executable code in the form of a delegate (which is a "method" call).

    After compilation you can then normally invoke the delegate - in your example the delegate is a Func. This approach might be needed when you dynamically create the expression tree based on data that is only available at run time with the end goal of creating and executing the corresponding delegate.

    You cannot see the "code" for the delegate. The expression tree itself which it is based on is the closest to that.

提交回复
热议问题