6

我正在使用 C# 和 Mono 的 Full AOT 技术为 iPhone 开发。根据他们的限制页面(链接文本),与传统的 Mono/.NET 不同,iPhone 上的代码是提前静态编译的,而不是由 JIT 编译器按需编译。

在硬件上运行时,出现以下异常:

ExecutionEngineException: Attempting to JIT compile method 'System.Reflection.MonoProperty:GetterAdapterFrame<Image, UnityEngine.Color> (System.Reflection.MonoProperty/Getter`2<Image, UnityEngine.Color>,object)' while running with --aot-only. 

System.Reflection.MonoProperty.GetValue (System.Object obj, System.Object[] index) [0x00000] 
Ani+AniValue.Get () 
Ani.CreateAnimations (System.Object obj, System.Collections.Hashtable properties, Single duration, System.Collections.Hashtable options, AniType type) 
Ani.Method (AniType type, System.Object obj, Single duration, System.Collections.Hashtable _properties, System.Collections.Hashtable _options) 
Ani.From (System.Object obj, Single duration, System.Collections.Hashtable _properties) 
xObject+<>c__CompilerGenerated5.MoveNext () 
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) 
xObject:StartAnimation(Animate, GameObject, Object, Object) 
SceneSplash:CreateBackground() 
SceneSplash:OnSetup() 
SceneSplash:OnSceneActivate(Callback) 
GameController:ActivateScene() 
GameController:DeactivateScene() 
GameController:SceneLoaded(Scene, GameObject, SceneBase) 
SceneBase:Start()

根据限制文档,不支持 System.Reflection.Emit,但他们声明作为 Reflection.Emit 的一面,“整个反射 API,包括 Type.GetType("someClass")、列出方法、列出属性、获取属性和价值观工作得很好。”

我已经包含了导致异常的代码......

void CreateAnimations(System.Object obj, Hashtable properties, float duration,
                      Hashtable options, AniType type)
{
    foreach (DictionaryEntry item in properties)
    {
        name = (string)item.Key;                  // Extract name and value
        System.Object value = item.Value;

        AniValue foo = new AniValue(obj, name);   // Create value object

        /* To exception occurs inside Get() */
        System.Object current = foo.Get();        // Get current value

        ...

上述方法从哈希表中获取属性名称,并使用它(连同 obj)来创建 AniValue 的实例。紧接着,调用 foo.Get() 来检索属性的值。异常发生在 propertyInfo.GetValue(obj, null) 上。

using System.Reflection

public class AniValue
{
    static BindingFlags bFlags = BindingFlags.Public | BindingFlags.NonPublic
                                 | BindingFlags.Instance | BindingFlags.Static;

    System.Object obj;  // Object a field or property is animated on
    string name;        // Name of the field or property

    System.Type objType;          // Type object
    FieldInfo fieldInfo;          // FieldInfo object
    PropertyInfo propertyInfo;    // PropertyInfo object

    public AniValue(System.Object o, string n)
    {
        obj = o;
        name = n;
        objType = obj.GetType();
        fieldInfo = objType.GetField(n, AniValue.bFlags);
        propertyInfo = objType.GetProperty(n, AniValue.bFlags);
        if (fieldInfo == null && propertyInfo == null)
        {
            throw new System.MissingMethodException("Property or field '" + n
                                                    + "' not found on " + obj);
        }
    }

    // Get field or property
    public System.Object Get()
    {
        if (propertyInfo != null)
        {
            /* The next line causes the Exception */
            return propertyInfo.GetValue(obj, null);
        }
        else
        {
            return fieldInfo.GetValue(obj);
        }
    }
    ...

虽然我在 C#、JIT、AOT 和反射方面的经验有限,但 GetValue() 是否应该触发 JIT?UnityEngine.Color 是一个结构体,Image 类是 xObject 的子类,xObject 是 UnityEngine.MonoBehaviour 的子类。颜色是 Image 的一个属性,这就是发生异常时代码可能获取的值。

有趣的是,您可以使用 .NET 1.1 编译代码,并且一切正常。只有当您使用 .NET 2.1 进行编译时,才会发生异常。

我不知道是否有解决方案或解决此问题的方法,但我会对有关原因的任何见解感兴趣。

4

1 回答 1

3

IIRC,通过反射也有关于泛型的警告。我相信它调用接口而不是具体类型,但同样适用 -特别是在使用反射时。

就个人而言,我只是在处理 iPhone 时放弃反思——这更容易。我仍在进行元编程,但我正在预生成常规代码(在完整框架上),然后我将其接管给 MonoTouch。它似乎工作得非常稳健。

于 2010-03-11T12:37:53.467 回答