在 Monotouch 中编译 lambda 并在设备上调用委托 [英] Compiling lambdas and invoking delegates on the device in Monotouch

查看:17
本文介绍了在 Monotouch 中编译 lambda 并在设备上调用委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 MonoTouch 中移植 .NET 代码库,并且我目前正在研究一种接收 Expression 的方法.我正在尝试编译它,然后动态调用它.

I am currently porting a .NET codebase in MonoTouch and I'm currently working on a method that receives an Expression<T>. I'm trying to compile it, and then dynamically invoke it.

这是我所做的:

// 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);

这在 iOS 模拟器中运行良好,结果12"打印在我的控制台中.但后来我在 iPad 上试了一下,收到了以下异常.

This works fine in the iOS Simulator, the result "12" is printed in my console. But then I tried it on an iPad, and I received the following exception.

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)

我做错了什么,我怎样才能让它发挥作用?

What am I doing wrong and how could I make it work?

推荐答案

对 System.Linq.Expressions 不熟悉,但大概涉及到运行时代码生成.

Not familiar with System.Linq.Expressions, but presumably it involves runtime code generation.

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

There is no JIT in iOS, all code must be compiled ahead-of-time. The same restriction does not apply in the simulator, hence your code runs there.

请参阅此处.

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

The Compile() method is not supported on the iOS device, since the device prevents the JIT engine from running. Compile itself is implemented using System.Reflection.Emit and this in turns requires a functioning JIT. So the above code would never wor kwith expression trees.

这篇关于在 Monotouch 中编译 lambda 并在设备上调用委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆