可以在monotouch中动态执行代码

本文关键字:动态 执行 代码 monotouch | 更新日期: 2023-09-27 18:06:42

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

提前致谢

可以在monotouch中动态执行代码

不可能。首先是因为Xamarin的局限性。iOS实际上是可行的(它不像一个普通的。net应用程序那样运行,而是编译成一个普通的iOS应用程序),因为苹果应用商店的安全模型。毕竟,如果一个应用程序的行为随时都可能改变,你就不能宣称它是安全的或符合法规的。

Since Xamarin。iOS 7.2版本对c#的动态特性有一些基本的支持。来自发行说明:

实验:c#动态支持。我们使得在Xamarin中动态使用c#成为可能。但是这个功能非常复杂,我们需要早期采采者告诉我们他们在其他平台上运行的动态代码,或者想要在Xamarin.iOS上运行。

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

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

当前你需要添加一个Microsoft.CSharp.dll作为依赖项——否则你会得到一个类似的异常:

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的对象现在工作:

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

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