在 iOS 上通过 C# IL2CPP 中的反射调用泛型方法

Mat*_*pii 2 c# reflection unity-game-engine ios il2cpp

这个问题专门针对 Unity3d IL2CPP 和 iOS。

使用反射调用泛型方法

class SourceValue<T> { public T value; }
class TargetValue<T> { public T value; }

static TargetValue<T> GenericMethod<T> (SourceValue<T> source) {
    return new TargetValue<T> { value = source.value };
}

void Main () {
    Type genericType = typeof(SourceValue<float>);
    Type typeArg = genericType.GenericTypeArguments[0];
    MethodInfo mi = GetType ().GetMethod ("GenericMethod", Flags | BindingFlags.Static);
    MethodInfo gmi = mi.MakeGenericMethod (typeArg);
    object src = new SourceValue<float> { value = 0.5f };
    object trg = gmi.Invoke (this, new object[] { src });
}
Run Code Online (Sandbox Code Playgroud)

这在 mac 上的 Unity 编辑器中运行时按预期工作。在 iOS 上调用失败并出现错误:

ExecutionEngineException: Attempting to call method 'GenericMethod<System.Single>' for which no ahead of time (AOT) code was generated.
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0  
Run Code Online (Sandbox Code Playgroud)

仅仅是 AOT 系统无法调用通用方法还是我遗漏了什么?

Jos*_*son 7

是的,因为 IL2CPP 是一个提前 (AOT) 编译器,它只适用于编译时存在的代码。这里没有代码GenericMethod<float>在“真正的”C# 源代码中使用,因此 IL2CPP 不知道生成相应的代码来使该实现工作。

这里真正的限制是泛型参数类型是float,它是一个值类型。string在这种情况下,您可以使用(引用类型)而不会出现任何问题。IL2CPP共享所有一般类型的具有一个通用的参数,它是一个引用类型(例如执行stringobject等等)。这是可能的,因为 C# 中的所有引用类型的大小都相同(IntrPtr.Size准确地说是 )。

所以这里的限制实际上有两个方面:

  1. IL2CPP 只能生成编译时知道的代码
  2. 当类型参数是值类型时,此限制仅适用于泛型类型。

请注意,理论上 IL2CPP 也可以与值类型泛型参数共享泛型类型的实现,尽管尚未实现。