Support interfaces inheriting interfaces better
This commit is contained in:
parent
d830929290
commit
8f5c165d39
|
@ -24,7 +24,7 @@ namespace PorygonSharp.UserData
|
|||
// ReSharper restore PrivateFieldCanBeConvertedToLocalVariable
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -52,12 +52,27 @@ namespace PorygonSharp.UserData
|
|||
{
|
||||
RegisterField(field);
|
||||
}
|
||||
|
||||
|
||||
var properties = Type.GetProperties(BindingFlags);
|
||||
foreach (var property in properties)
|
||||
{
|
||||
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);
|
||||
foreach (var method in methods)
|
||||
|
|
|
@ -99,6 +99,30 @@ namespace PorygonSharp.UserData
|
|||
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)
|
||||
{
|
||||
var hash = GetTypeId(t);
|
||||
|
|
Loading…
Reference in New Issue