using System.Linq.Expressions;
using FastExpressionCompiler;
public static class Numbers
public static int GetInt() => 40;
public static int AddTwo(ref int value) => value + 2;
public static void Main()
var useFastCompile = true;
var result1 = CompileAndExecute( CallWithAssign(), useFastCompile );
Console.WriteLine( $"Call Assign: {result1}" );
var result2 = CompileAndExecute( CallInline(), useFastCompile );
Console.WriteLine( $"Call Inline: {result2}");
public static int CompileAndExecute( Expression expr, bool useFastCompile )
var lambda = useFastCompile
? Expression.Lambda<Func<int>>(expr).CompileFast()
: Expression.Lambda<Func<int>>(expr).Compile();
public static Expression CallInline()
var getIntMethod = typeof(Numbers).GetMethod(nameof(Numbers.GetInt));
var addTwoMethod = typeof(Numbers).GetMethod(nameof(Numbers.AddTwo));
var getIntCall = Expression.Call(getIntMethod);
return Expression.Call(addTwoMethod, getIntCall);
public static Expression CallWithAssign()
var getIntMethod = typeof(Numbers).GetMethod(nameof(Numbers.GetInt));
var addTwoMethod = typeof(Numbers).GetMethod(nameof(Numbers.AddTwo));
var getIntCall = Expression.Call(getIntMethod);
var variable = Expression.Variable(typeof(int), "local");
var assignToVar = Expression.Assign(variable, getIntCall);
var addTwoCall = Expression.Call(addTwoMethod, variable);
var block = Expression.Block(