Pregunta

Estoy desarrollando para el iPhone usando C # y tecnología Full AOT de Mono. De acuerdo con su Limitaciones página ( enlace de texto ), a diferencia tradicional Mono /. NET, el código en el iPhone es compilado estáticamente antes de tiempo en lugar de ser compilado en la demanda por un compilador JIT.

Cuando se ejecuta en el hardware, se produce la siguiente excepción:

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()

Según el documento Limitaciones, System.Reflection.Emit no está soportado, pero se afirma que como lado de Reflection.Emit "todo el API de reflexión, incluyendo Type.GetType (" algunaClase "), lista de métodos, listando propiedades , atributos y valores de ir a buscar funciona muy bien ".

He incluido el código que está causando la excepción ...

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

        ...

El método anterior toma un nombre de propiedad de una tabla hash, y la utiliza (junto con obj) para crear una instancia de AniValue. Justo después, foo.Get () se llama para recuperar el valor de la propiedad. La excepción se produce en 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);
        }
    }
    ...

A pesar de que he experiencia con C #, JIT, AOT y la reflexión, en caso de GetValue () JIT gatillo limitado? UnityEngine.Color es una estructura, y la clase La imagen es como subclase de XObject, que es una subclase de UnityEngine.MonoBehaviour. El color es una propiedad de imagen, y que es lo que el código podría estar recibiendo el valor de cuando se produce la excepción.

Curiosamente, puede compilar el código utilizando .NET 1.1, y todo lo ejecuta bien. Sólo cuando se compila utilizando .NET 2.1 se produce la excepción.

No sé si hay una solución o trabajar alrededor de esto, pero yo estaría interesado en cualquier penetración en cuanto a la causa.

¿Fue útil?

Solución

IIRC, hay también una advertencia sobre los medicamentos genéricos a través de la reflexión. I creer que llama a las interfaces en lugar de tipos concretos, pero el mismo puede aplicarse -. En especialmente cuando se utiliza la reflexión

En lo personal, sólo estoy dejando caer la reflexión cuando se trata de iPhone - es más fácil. Todavía estoy haciendo meta-programación, pero estoy pre-generar código regular (en el marco completo) que luego tomo a MonoTouch. Parece que funciona bastante robusta.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top