I am confused by some Reflection code and was looking for understanding

I am developing C # and Mono Full AOT technology for iPhone. According to the "Limitations" page ( link text ), unlike traditional Mono / .NET, iPhone code is statically compiled ahead of time, rather than compiled upon request by the JIT compiler.

When running on hardware, the following exception occurs:

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

According to the document "Constraints" System.Reflection.Emit is not supported, but they indicate that aside from Reflection.Emit "the entire Reflection API, including Type.GetType (" someClass "), listing methods, list properties, attribute selection and values ​​works just fine. "

I have included code that throws an exception ...

 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 ... 

The above method captures the property name from the hash table and uses it (along with obj) to instantiate AniValue. Immediately after this, foo.Get () is called to retrieve the value of the property. An exception occurs in 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); } } ... 

Although I have limited experience with C #, JIT, AOT, and Reflection, should GetValue () run JIT? UnityEngine.Color is a structure, and the Image class is a subclass of xObject, which is a subclass of UnityEngine.MonoBehaviour. Color is an Image property, and it is this code that can get the value when an exception occurs.

Interestingly, you can compile the code using .NET 1.1, and everything will be fine. Only compilation using .NET 2.1 throws an exception.

I don’t know if there is a solution or what is connected with it, but I would be interested to know about the reason.

+6
reflection c # iphone mono unity3d
source share
1 answer

IIRC, there is also a warning about generics through reflection. I believe that it calls interfaces, not specific types, but the same thing can apply - especially when using reflection.

Personally, I just throw away the reflection when working with the iPhone - it's easier. I'm still doing metaprogramming, but I pre-create the usual code (in the full structure), which I then take on MonoTouch. It seems to be working pretty confidently.

+3
source share

All Articles