Xamarin.iOS上的MakeGenericMethod / MakeGenericType [英] MakeGenericMethod/MakeGenericType on Xamarin.iOS

查看:109
本文介绍了Xamarin.iOS上的MakeGenericMethod / MakeGenericType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚从Xamarin部署iOS时的限制真正意味着什么。

I'm trying to figure out what the limitations really means when deploying for iOS from Xamarin.

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

I是因为你没有JIT,因此任何MakeGenericMethod或MakeGenericType都不会起作用,因为需要JIT编译。

I was under the impression that you have no JIT and thus any MakeGenericMethod or MakeGenericType would NOT work as that would require JIT compilation.

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

Also I understood that when running on the simulator, these restrictions does not apply since the simulator is not running in the full AOT (Ahead of Time) mode.

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

After setting up my Mac so that I could deploy to my phone, I would except the following test to fail when running on the actual device (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编译吗?

The thing is that completes successfully and I can't really understand why. Does not this code require JIT compilation?

为了让它破裂,我还用MakeGenericType进行了测试。

In an effort to "make it break" , I also did a test with 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)
    {

    }
}

如果没有JIT,这怎么办?

How can this work when there is no JIT?

我错过了什么吗?

推荐答案

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

In order to make the code fail go to iOS project options, tab "iOS Build" and change the "Linker Behavior:" to "Link all assemblies". Running the code will result in Exception and it will be of type default constructor for type XXX was not found.

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

Now, make a reference to the SomeGenericClass{string} in your code and the method will run just fine. The two added lines cause the compiler to include SomeGenericClass{string} in the binary. Note that the lines can be anywhere in the application that is compiled into the binary, they don't have to be in the same function.

    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"});
    }

这篇关于Xamarin.iOS上的MakeGenericMethod / MakeGenericType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆