vendredi 9 octobre 2020

Get private field from class that inherits from a generic class via reflection in C# [duplicate]

I have a following situation. I'm using an external SerializableDictionary for Unity (but that's only to give you some background). Now I need to get m_keys field via reflection from a MyDictionary instance but the GetField() returns null and I have no idea why.

[Serializable]
public class MyDictionary : SerializableDictionary<ClassA, ClassB>
{

}


public class SerializableDictionary<TKey, TValue> : SerializableDictionaryBase<TKey, TValue, TValue>
{

}

public abstract class SerializableDictionaryBase<TKey, TValue, TValueStorage> : SerializableDictionaryBase, IDictionary<TKey, TValue>, IDictionary, ISerializationCallbackReceiver, IDeserializationCallback, ISerializable
{
    Dictionary<TKey, TValue> m_dict;

    [SerializeField]
    TKey[] m_keys;

    [SerializeField]
    TValueStorage[] m_values;
}

I'm trying to retrieve this field using following code:

var type = _propertyObject.GetType();

var field = type.GetField(pathNode, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

where pathNode = "m_keys" and _propertyObject = MyDictionary





Aucun commentaire:

Enregistrer un commentaire