mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 17:26:02 +08:00
Fixed #103
Similar issue as #100 GetField was not returning private fields. The method now not only looks for private fields, but also fields inside inherited classes
This commit is contained in:
parent
84e2af7916
commit
3a8ae366f2
@ -154,7 +154,7 @@ namespace XNodeEditor {
|
||||
|
||||
private static System.Type GetType(SerializedProperty property) {
|
||||
System.Type parentType = property.serializedObject.targetObject.GetType();
|
||||
System.Reflection.FieldInfo fi = parentType.GetField(property.propertyPath);
|
||||
System.Reflection.FieldInfo fi = NodeEditorWindow.GetFieldInfo(property.serializedObject.targetObject.GetType(), property.name);
|
||||
return fi.FieldType;
|
||||
}
|
||||
|
||||
|
||||
@ -61,6 +61,15 @@ namespace XNodeEditor {
|
||||
return widths;
|
||||
}
|
||||
|
||||
/// <summary> Get FieldInfo of a field, including those that are private and/or inherited </summary>
|
||||
public static FieldInfo GetFieldInfo(Type type, string fieldName) {
|
||||
// If we can't find field in the first run, it's probably a private field in a base class.
|
||||
FieldInfo field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
// Search base classes for private fields only. Public fields are found above
|
||||
while (field == null && (type = type.BaseType) != typeof(XNode.Node)) field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
return field;
|
||||
}
|
||||
|
||||
/// <summary> Get all classes deriving from baseType via reflection </summary>
|
||||
public static Type[] GetDerivedTypes(Type baseType) {
|
||||
List<System.Type> types = new List<System.Type>();
|
||||
|
||||
@ -36,9 +36,7 @@ namespace XNodeEditor {
|
||||
|
||||
public static bool GetAttrib<T>(Type classType, string fieldName, out T attribOut) where T : Attribute {
|
||||
// If we can't find field in the first run, it's probably a private field in a base class.
|
||||
FieldInfo field = classType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
// Search base classes for private fields only. Public fields are found above
|
||||
while (field == null && (classType = classType.BaseType) != typeof(XNode.Node)) field = classType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
FieldInfo field = NodeEditorWindow.GetFieldInfo(classType, fieldName);
|
||||
// This shouldn't happen. Ever.
|
||||
if (field == null) {
|
||||
Debug.LogWarning("Field " + fieldName + " couldnt be found");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user