MakeGenericMethod / MakeGenericType在Xamarin.iOS上

我试图弄清楚从Xamarin部署iOS时限制的真正含义。

http://developer.xamarin.com/guides/ios/advanced_topics/limitations/

我的印象是,你没有JIT,因此任何MakeGenericMethod或MakeGenericType都不会工作,因为这将需要JIT编译。

另外我明白,当在模拟器上运行时,这些限制不适用,因为模拟器没有在完整的AOT(前面的时间)模式下运行。

设置我的Mac后,我可以部署到我的手机,除了以下testing失败时,在实际设备(iPhone)上运行。

[Test] public void InvokeGenericMethod() { var method = typeof(SampleTests).GetMethod ("SomeGenericMethod"); var closedMethod = method.MakeGenericMethod (GetTypeArgument()); closedMethod.Invoke (null, new object[]{42}); } public static void SomeGenericMethod<T>(T value) { } private Type GetTypeArgument() { return typeof(int); } 

事情是成功完成,我不明白为什么。 这个代码不需要JIT编译吗?

为了“让它rest”,我也用MakeGenericType做了testing。

  [Test] public void InvokeGenericType() { var type = typeof(SomeGenericClass<>).MakeGenericType (typeof(string)); var instance = Activator.CreateInstance (type); var method = type.GetMethod ("Execute"); method.Invoke (instance, new object[]{"Test"}); } public class SomeGenericClass<T> { public void Execute(T value) { } } 

在没有JIT的情况下如何工作?

我错过了什么吗?

为了使代码失败转到iOS项目选项,选项卡“iOS构build”,并将“链接器行为:”更改为“链接所有程序集”。 运行代码将导致exception,它将是XXXtypes的默认构造函数未find。

现在,在代码中对SomeGenericClass {string}进行引用,该方法将运行得很好。 两个添加的行使编译器在二进制文件中包含SomeGenericClass {string}。 请注意,行可以在编译到二进制文件的应用程序中的任何位置,它们不必位于同一个函数中。

  public void InvokeGenericType() { // comment out the two lines below to make the code fail var strClass = new SomeGenericClass<string>(); strClass.Execute("Test"); var type = typeof(SomeGenericClass<>).MakeGenericType (typeof(string)); var instance = Activator.CreateInstance (type); var method = type.GetMethod ("Execute"); method.Invoke (instance, new object[]{"Test"}); }