Search code examples
iosxamarinil

Why does LambdaExpression.Compile() work on iOS (Xamarin)?


Since Xamarin.iOS doesn't support code generation at runtime, why do Compile() and DynamicInvoke() work as expected?

For example, the following code works fine:

var lambda = Expression.Lambda(
                          Expression.Add(
                              Expression.Constant(1),
                              Expression.Constant(2)
                          )
             );

var f = lambda.Compile();
var result = f.DynamicInvoke();

// result==3 at this point

Is Xamarin evaluating the expression tree at runtime instead of emitting IL code?


Solution

  • On platforms that support code generation, Reflection.Emit-based LambdaCompiler is used.

    If that's not available, the expression is interpreted using the interpreter. For example, there are classes that interpret Constant and Add.