Can execute Code Dynamically in monotouch?

后端 未结 2 1437
野趣味
野趣味 2020-12-18 06:40

In C# .net we can run code dynamically by using System.Codedom.Provider. Like the same is there any possibility to execute the code dynamically in Monotouch (iP

2条回答
  •  暖寄归人
    2020-12-18 07:08

    Since Xamarin.iOS version 7.2 there is some basic support for C#'s dynamic feature. From the release notes:

    Experimental: C# dynamic support. We made it possible to use C# dynamic with Xamarin.iOS but the feature is very complex and we need early adopters let us know what dynamic code they run on other platforms or would like to run on Xamarin.iOS.

    I've successfully compiled and executed dynamic access of anonymous types:

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

    Currently you need to add a Microsoft.CSharp.dll as a dependency -- otherwise you'll get an exception similar to this:

    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)
    

    Unfortunately neither ExpandoObject nor Json.Net's JObject work right now:

    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
    

    I've created two bug reports for this: https://bugzilla.xamarin.com/show_bug.cgi?id=20081 and https://bugzilla.xamarin.com/show_bug.cgi?id=20082.

提交回复
热议问题