diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs index 2eaa6a882..5c817183b 100644 --- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs @@ -1,14 +1,32 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Discord.Commands { - internal class ReflectionUtils + internal static class ReflectionUtils { + private static readonly TypeInfo objectTypeInfo = typeof(object).GetTypeInfo(); + internal static T CreateObject(TypeInfo typeInfo, CommandService service, IDependencyMap map = null) => CreateBuilder(typeInfo, service)(map); + private static System.Reflection.PropertyInfo[] GetProperties(TypeInfo typeInfo) + { + var result = new List(); + while (typeInfo != objectTypeInfo) + { + foreach (var prop in typeInfo.DeclaredProperties) + { + if (prop.SetMethod?.IsPublic == true && prop.GetCustomAttribute() == null) + result.Add(prop); + } + typeInfo = typeInfo.BaseType.GetTypeInfo(); + } + return result.ToArray(); + } + internal static Func CreateBuilder(TypeInfo typeInfo, CommandService service) { var constructors = typeInfo.DeclaredConstructors.Where(x => !x.IsStatic).ToArray(); @@ -19,7 +37,7 @@ namespace Discord.Commands var constructor = constructors[0]; System.Reflection.ParameterInfo[] parameters = constructor.GetParameters(); - System.Reflection.PropertyInfo[] properties = typeInfo.DeclaredProperties + System.Reflection.PropertyInfo[] properties = GetProperties(typeInfo) .Where(p => p.SetMethod?.IsPublic == true && p.GetCustomAttribute() == null) .ToArray();