Search Unity

Question Type.GetField problem.

Discussion in 'Editor & General Support' started by georgeq, Aug 6, 2022.

  1. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    662
    I'm writing a property drawer for a generic class, thus I need to know the generic type I'm editing, for that I'm using this code:

    Code (CSharp):
    1.  
    2. Type      parentType = property.serializedObject.targetObject.GetType();
    3. FieldInfo fi         = parentType.GetField(property.propertyPath,BindingFlags.NonPublic);
    4. Type      t          = fi.FieldType;
    5. if(t.IsGenericType) {
    6.    parameters = t.GenericTypeArguments;
    7. } else {
    8.    parameters = new Type[0];
    9. }
    But it only works if the property is public and no BindingFlags are provided to GetField... I have tried everything I've thought of, but just can't make it work with private properties... any ideas?