From 8f5c165d39e9bb043189e00613f1557e00279a73 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 7 Sep 2019 12:17:58 +0200 Subject: [PATCH] Support interfaces inheriting interfaces better --- PorygonSharp/UserData/UserData.cs | 19 +++++++++++++++++-- PorygonSharp/UserData/UserDataHandler.cs | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/PorygonSharp/UserData/UserData.cs b/PorygonSharp/UserData/UserData.cs index b1ae19f..e0e070f 100644 --- a/PorygonSharp/UserData/UserData.cs +++ b/PorygonSharp/UserData/UserData.cs @@ -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) diff --git a/PorygonSharp/UserData/UserDataHandler.cs b/PorygonSharp/UserData/UserDataHandler.cs index e1b54a0..b419524 100644 --- a/PorygonSharp/UserData/UserDataHandler.cs +++ b/PorygonSharp/UserData/UserDataHandler.cs @@ -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);