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

Sri*_*Sri 4 c# iphone xamarin.ios ipad

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

提前致谢,

Mar*_*een 6

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

  • Monodroid和Monotouch的工作方式截然不同.正如您在Monodroid中看到的那样,在Monotouch中有一个实际的Mono运行http://docs.xamarin.com/guides/android/advanced_topics/architecture,它只是编译为普通iOS而没有任何JIT功能http://docs.xamarin .com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/part_1 _-_ understanding_the_xamarin_mobile_platform#1.1.compilation.Mono(和.NET)中的动态编码需要JIT. (2认同)

Rod*_*dja 6

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

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

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

 dynamic d = new { name = "x" };
 tmp = d.name;
Run Code Online (Sandbox Code Playgroud)

目前,您需要添加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)
Run Code Online (Sandbox Code Playgroud)

不幸的是,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
Run Code Online (Sandbox Code Playgroud)

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