可以在monotouch中动态执行代码吗?

在C#.net中,我们可以使用System.Codedom.Provider动态运行代码。 同样有可能在Monotouch(iPhone / iPad)中动态执行代码。

提前致谢,

不可能。 首先是因为Xamarin.iOS 实际上如何工作的限制(它不像普通的.NET应用程序那样运行,而是编译成普通的iOS应用程序)并且因为Apple Appstore中的安全模型。 毕竟,如果行为可能随时发生变化,您无法宣布应用程序是安全的或符合规定。

从Xamarin.iOS版本7.2开始,对C#的动态特性有一些基本的支持。 从发行说明 :

实验:C#动态支持。 我们可以将C#dynamic与Xamarin.iOS一起使用,但function非常复杂,我们需要早期采用者告诉我们他们在其他平台上运行的动态代码,或者想在Xamarin.iOS上运行。

我已经成功编译并执行了匿名类型的动态访问:

  dynamic d = new { name = "x" }; tmp = d.name; 

目前,您需要添加Microsoft.CSharp.dll作为依赖项 – 否则您将获得类似于此的exception:

 Error CS0518: The predefined type `Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported (CS0518) (DynamicSupportOniOS) Error CS1969: Dynamic operation cannot be compiled without `Microsoft.CSharp.dll' assembly reference (CS1969) (DynamicSupportOniOS) 

不幸的是,ExpandoObject和Json.Net的JObject现在都不起作用:

 dynamic x = new ExpandoObject(); x.NewProp = "demo"; // this still works Console.WriteLine(x.NewProp); // fails with Xamarin.iOS 7.2 dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}"); Console.WriteLine(d.number); // fails with Xamarin.iOS 7.2 

我为此创建了两个错误报告: https : //bugzilla.xamarin.com/show_bug.cgi?id = 2004和https://bugzilla.xamarin.com/show_bug.cgi?id=20082 。