write our own implementation of GetField that actually flattens the INSTANCE hierarchy

This commit is contained in:
Michael Becker 2020-01-06 00:23:28 -05:00
parent f5c2801e70
commit dc40681c7e
No known key found for this signature in database
GPG Key ID: 506F54899E2BFED7

View File

@ -116,5 +116,34 @@ namespace MBS.Framework
return mvarAvailableTypes;
}
public static FieldInfo GetField(Type type, string name, BindingFlags flags)
{
FieldInfo fi = type.GetField(name, flags);
if (fi == null)
{
if ((flags & BindingFlags.FlattenHierarchy) == BindingFlags.FlattenHierarchy)
{
Type typObj = typeof(object);
// this is the way FlattenHierarchy SHOULD work.. for Instance binding
while (type.BaseType != typObj)
{
fi = GetField(type.BaseType, name, flags);
if (fi != null) return fi; // we found it
}
}
}
return fi;
}
public static void SetField(object obj, string name, BindingFlags flags, object value)
{
if (obj == null) return;
FieldInfo fi = GetField(obj.GetType(), name, flags);
if (fi != null)
{
fi.SetValue(obj, value);
}
}
}
}