Support interfaces inheriting interfaces better

This commit is contained in:
Deukhoofd 2019-09-07 12:17:58 +02:00
parent d830929290
commit 8f5c165d39
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 41 additions and 2 deletions

View File

@ -24,7 +24,7 @@ namespace PorygonSharp.UserData
// ReSharper restore PrivateFieldCanBeConvertedToLocalVariable // ReSharper restore PrivateFieldCanBeConvertedToLocalVariable
private const BindingFlags BindingFlags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | private const BindingFlags BindingFlags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Static; System.Reflection.BindingFlags.Static;
public UserData(uint id, Type type) public UserData(uint id, Type type)
{ {
@ -52,12 +52,27 @@ namespace PorygonSharp.UserData
{ {
RegisterField(field); RegisterField(field);
} }
var properties = Type.GetProperties(BindingFlags); var properties = Type.GetProperties(BindingFlags);
foreach (var property in properties) foreach (var property in properties)
{ {
RegisterProperty(property); RegisterProperty(property);
} }
var t = Type;
if (t.IsInterface)
{
// GetProperties doesn't return inherited properties if type is an interface.
var parentTypes = t.GetInterfaces();
foreach (var parentType in parentTypes)
{
properties = parentType.GetProperties(BindingFlags);
foreach (var property in properties)
{
RegisterProperty(property);
}
}
}
var methods = Type.GetMethods(BindingFlags); var methods = Type.GetMethods(BindingFlags);
foreach (var method in methods) foreach (var method in methods)

View File

@ -99,6 +99,30 @@ namespace PorygonSharp.UserData
return ReverseLookup.ContainsKey(id); return ReverseLookup.ContainsKey(id);
} }
public static bool TryResolveType(Type t)
{
var cur = t;
while (cur != null)
{
if (IsTypeRegistered(cur))
{
UserDataLookup.TryAdd(t, UserDataLookup[cur]);
return true;
}
cur = cur.BaseType;
}
var interfaces = t.GetInterfaces();
foreach (var i in interfaces)
{
if (!IsTypeRegistered(i))
continue;
UserDataLookup.TryAdd(t, UserDataLookup[i]);
return true;
}
return false;
}
public static int GetUserDataFieldCount(Type t) public static int GetUserDataFieldCount(Type t)
{ {
var hash = GetTypeId(t); var hash = GetTypeId(t);