Skip to content

Commit

Permalink
Merge pull request #48 from JohannesDeml/bugfix/propertydrawer-allow-…
Browse files Browse the repository at this point in the history
…inheritance

Add logic to check for inherited PropertyDrawers from base classes and interfaces
  • Loading branch information
mackysoft authored Feb 16, 2024
2 parents 69830f3 + 70f2cda commit e5a5157
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static bool TryGetPropertyDrawer (Type type,out PropertyDrawer drawer)

static Type GetCustomPropertyDrawerType (Type type)
{
Type[] interfaceTypes = type.GetInterfaces();

foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type drawerType in assembly.GetTypes())
Expand All @@ -36,9 +38,39 @@ static Type GetCustomPropertyDrawerType (Type type)
if (field != null)
{
var fieldType = field.GetValue(customPropertyDrawer) as Type;
if (fieldType != null && fieldType == type)
if (fieldType != null)
{
return drawerType;
if (fieldType == type)
{
return drawerType;
}

// If the property drawer also allows for being applied to child classes, check if they match
var useForChildrenField = customPropertyDrawer.GetType().GetField("m_UseForChildren", BindingFlags.NonPublic | BindingFlags.Instance);
if (useForChildrenField != null)
{
object useForChildrenValue = useForChildrenField.GetValue(customPropertyDrawer);
if (useForChildrenValue is bool && (bool)useForChildrenValue)
{
// Check interfaces
if (Array.Exists(interfaceTypes, interfaceType => interfaceType == fieldType))
{
return drawerType;
}

// Check derived types
Type baseType = type.BaseType;
while (baseType != null)
{
if (baseType == fieldType)
{
return drawerType;
}

baseType = baseType.BaseType;
}
}
}
}
}
}
Expand Down

0 comments on commit e5a5157

Please sign in to comment.