【发布时间】:2014-07-05 15:50:24
【问题描述】:
我试图弄清楚从 Xamarin 为 iOS 部署时限制的真正含义。
http://developer.xamarin.com/guides/ios/advanced_topics/limitations/
我的印象是您没有 JIT,因此任何 MakeGenericMethod 或 MakeGenericType 都无法工作,因为这需要 JIT 编译。
我还了解到,在模拟器上运行时,这些限制不适用,因为模拟器没有在完全 AOT(提前)模式下运行。
设置我的 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);
}
事情是成功完成,我真的不明白为什么。这段代码不需要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)
{
}
}
在没有 JIT 的情况下如何工作?
我错过了什么吗?
【问题讨论】:
-
该代码可以进行 AOT 编译,因此可以毫无问题地工作。您将遇到限制的地方是生成 IL 代码。代码生成适用于 Android 和 Windows Phone,但不适用于 iOS。
-
根据这篇文章,MakeGenericType 将导致 MSIL 被动态生成。 msdn.microsoft.com/en-us/magazine/cc163610.aspx如果真是这样,我不明白它是如何工作的。
-
我实际上是错的,因为代码会因激进的链接选项而失败(除非代码中的其他地方有对 SomeGenericClass{string} 的引用)。请在下面使用一些示例代码查看我的答案。
标签: c# ios iphone xamarin.ios xamarin