C# 在Monotouch中编译lambda并调用设备上的委托

C# 在Monotouch中编译lambda并调用设备上的委托,c#,delegates,lambda,xamarin.ios,expression-trees,C#,Delegates,Lambda,Xamarin.ios,Expression Trees,我目前正在MonoTouch中移植.NET代码库,并且正在开发一种方法,该方法接收表达式。我试图编译它,然后动态调用它 以下是我所做的: // Here's an example of what I could receive Expression<Action<int>> expression = (a => Console.WriteLine (a * 2)); // And here's what I'm trying to do to invoke it

我目前正在MonoTouch中移植.NET代码库,并且正在开发一种方法,该方法接收
表达式。我试图编译它,然后动态调用它

以下是我所做的:

// Here's an example of what I could receive
Expression<Action<int>> expression = (a => Console.WriteLine (a * 2));

// And here's what I'm trying to do to invoke it
expression.Compile().DynamicInvoke(6);

我做错了什么?我怎样才能使它工作?

不熟悉System.Linq.Expressions,但可能涉及运行时代码生成

iOS中没有JIT,所有代码都必须提前编译。同样的限制不适用于模拟器,因此您的代码在那里运行


iOS设备不支持
Compile()
方法,因为该设备会阻止JIT引擎运行。编译本身是使用System.Reflection.Emit实现的,这反过来又需要一个正常运行的JIT。因此,上面的代码永远不会使用表达式树。

我猜操作系统不会阻止代码的生成,而是阻止将其标记为可执行文件。当然,最终结果是一样的。
Object reference not set to an instance of an object
   at System.Linq.jvm.Runner.CreateDelegate ()
   at System.Linq.Expressions.LambdaExpression.Compile ()
   at System.Linq.Expressions.Expression`1[System.Action`1[System.Int32]].Compile ()
   at TestSolution2.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options)