私はいくつかのリフレクションコードについて困惑している、そして洞察を探して
-
19-09-2019 - |
質問
私はC#とMonoのフル・AOT技術を使用してiPhoneのために開発しています。彼らのの制限事項のページ(リンクテキストの)によると、伝統的なモノとは異なり、/。 iPhone上のNET、コードを静的に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、「(工ass」)Type.GetType含む全体リフレクションAPI」から側とメソッドをリストすることを述べます、フェッチ属性と値が正常に動作します。 "
私は例外を引き起こしているコードを含めました...
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
...
上記の方法は、ハッシュテーブルからプロパティ名をつかみ、そしてAniValueのインスタンスを作成する(OBJと一緒に)それを使用します。ただ、その後、foo.Getは()プロパティの値を取得するために呼び出されます。例外はpropertyInfo.GetValue(ヌル、OBJ)で発生します。
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クラスはUnityEngine.MonoBehaviourのサブクラスであるのXObjectのサブクラスとしてあります。色は画像のプロパティであり、それは、コードが例外が発生したときの値を取得することができるものである。
興味深いことに、あなたは.NET 1.1を使用してコードをコンパイルすることができ、すべてが正常に実行されます。あなたは.NET 2.1を使用してコンパイルする場合にのみ例外が発生します。
これに対する周りの溶液または仕事があるかどうか、私は知りませんが、私は、原因のように任意の洞察力に興味があると思います。
解決
IIRC、ジェネリック医薬品に関する警告は、リフレクション経由でもあります。私は<全角> のそれはむしろ、具体的なタイプよりも、インターフェースを呼び出しますが、同じことが適用される場合があります信じて - 。の特定のの反射を使用しているときに
個人的には、iPhoneを扱うとき、私はちょうど反射を落としている - それは簡単です。私はまだメタプログラミングをやっているが、私はその後、MonoTouchでに引き継ぐ(フルフレームワーク上の)定期的なコードを事前に生成しています。かなり確実に動作するようです。