c# - Expression.Compile 在 Monotouch 上做了什么?

标签 c# xamarin.ios xamarin expression

所以 Expression.Compile做以下事情

Compiles the lambda expression described by the expression tree into executable code and produces a delegate that represents the lambda expression.

它在可移植类库中可用。

然而,当通过 Monotouch 运行 .net 时 dynamic code generation is not supported

Since the iPhone's kernel prevents an application from generating code dynamically Mono on the iPhone does not support any form of dynamic code generation.

因此基于 IOS 上的 Xamarin 不支持 Expression.Compile。

那么当您在 IOS 上调用 Expression.Compile Xamarin 时会发生什么?它会抛出异常吗?如果是,是什么异常?是否在任何地方记录了它?

最佳答案

代码是使用 AOT 选项编译的,因此它实际上不会在运行时编译(我不知道 Compile() 后台发生的细节)。 Microsoft 文档中的示例在 iOS 设备上运行良好,无一异常(exception)。

    public override void FinishedLaunching(UIApplication application)
    {
        System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
        // Compile the expression tree into executable code.
        Func<int, bool> deleg = expr.Compile();
        // Invoke the method and print the output.
        Console.WriteLine("deleg(4) = {0}", deleg(4));
    }

您不能在运行时创建 IL 代码 (System.Reflection.Emit),并且对某些链接器选项使用反射也有限制,some more info on this thread.可能存在不进行 AOT 编译的表达式,在这些情况下,您会在运行时遇到关于尝试使用仅 AOT 选项进行 JIT 编译的异常。

关于c# - Expression.Compile 在 Monotouch 上做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24977939/

相关文章:

c# - 使用 stub 和模拟的正确方法是什么?

c# - 多种类型的扩展方法

c# - MonoTouch - 字段与自动属性

Xamarin Forms AppCenter 崩溃,发送到类的选择器无法识别

c# - 在 Xamarin iOS 应用程序中使用 UIApplicationDelegate.DidReceiveRemoteNotification 处理远程通知

c# - 为什么缓存的正则表达式优于编译的正则表达式?

android - 用于以编程方式更新 apk 的 Xamarin 应用程序

android - 缩小照片尺寸

mysql - SQL 查询 - 提取带有空值的查询

c# - StringTemplate与StringTemplateGroup