Xamarin.iOS上的MakeGenericMethod/MakeGenericType

see*_*per 5 c# iphone xamarin.ios ios xamarin

我试图找出从Xamarin部署iOS时的限制真正意义.

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

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

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

在设置我的Mac以便我可以部署到我的手机之后,除了以下测试在实际设备(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);
    }
Run Code Online (Sandbox Code Playgroud)

问题是成功完成,我无法理解为什么.这段代码不需要JIT编译吗?

为了"打破它",我还使用MakeGenericType进行了测试.

    [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)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

当没有JIT时,它如何工作?

我错过了什么吗?

SKa*_*all 4

为了使代码失败,请转到 iOS 项目选项,选项卡“iOS Build”并将“链接器行为:”更改为“链接所有程序集”。运行代码将导致异常,并且它将是未找到类型 XXX 的默认构造函数类型。

现在,在代码中引用 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"});
    }
Run Code Online (Sandbox Code Playgroud)