Skip to content

Expression Compiler

Akash Kava edited this page Oct 13, 2021 · 3 revisions

Getting Started

All expressions are prefixed with Y, and can be constructed easily as shown below.

var a = YExpression.Parameter(typeof(int));
var b = YExpression.Parameter(typeof(int));

var exp = YExpression.Lambda<Func<int,int,int>>("add",
            YExpression.Binary(a, YOperator.Add, b),
            new YParameterExpression[] { a, b });

var fx = exp.Compile();

Assert.AreEqual(1, fx(1, 0));
Assert.AreEqual(3, fx(1, 2));

Methods

Compile

This method will compile expression to IL in memory, it will not analyze nested lambdas, this is by design to generate IL faster. For nested lambda, please use CompileWithNestedLambdas method. It will return DynamicMethod delegate.

CompileWithNestedLambdas

When you expression tree contains nested lambda with closures, use this method to generate the IL. It will return DynamicMethod delegate.

CompileInAssembly

This is similar to CompileToMethod, this method generate an empty assembly and it will return generated method from MethodBuilder.

CompileToStaticMethod

This is same as CompileToMethod of Linq.

CompileToInstanceMethod

You can compile lambda with this parameter to an instance method.

Clone this wiki locally