我试图弄清楚从 Xamarin 部署 iOS 时这些限制的真正含义。
http://developer.xamarin.com/guides/ios/advanced_topics/limitations/ 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,这怎么行?
我错过了什么吗?