From df6579260ee69045fefcc8917c86b805ed6deea3 Mon Sep 17 00:00:00 2001 From: Khionu Sybiern Date: Wed, 1 Mar 2017 07:13:38 -0500 Subject: [PATCH 1/4] Fix detection of IDependencyMap impl Not pretty, but it works. --- src/Discord.Net.Commands/Utilities/ReflectionUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs index 1333b9640..b8fb1f64a 100644 --- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs @@ -58,7 +58,7 @@ namespace Discord.Commands { if (targetType == typeof(CommandService)) arg = service; - else if (targetType == typeof(IDependencyMap)) + else if (targetType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDependencyMap))) arg = map; else throw new InvalidOperationException($"Failed to create \"{baseType.FullName}\", dependency \"{targetType.Name}\" was not found."); From c350debdbaacb6f3ba75e38b962831954787a97e Mon Sep 17 00:00:00 2001 From: Khionu Sybiern Date: Wed, 1 Mar 2017 08:08:07 -0500 Subject: [PATCH 2/4] Better implimentation of detection --- src/Discord.Net.Commands/Utilities/ReflectionUtils.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs index b8fb1f64a..fdbc8d015 100644 --- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs @@ -51,6 +51,8 @@ namespace Discord.Commands }; } + private static readonly TypeInfo _dependencyTypeInfo = typeof(IDependencyMap).GetTypeInfo(); + internal static object GetMember(Type targetType, IDependencyMap map, CommandService service, TypeInfo baseType) { object arg; @@ -58,7 +60,7 @@ namespace Discord.Commands { if (targetType == typeof(CommandService)) arg = service; - else if (targetType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDependencyMap))) + else if (_dependencyTypeInfo.IsAssignableFrom(targetType.GetTypeInfo())) arg = map; else throw new InvalidOperationException($"Failed to create \"{baseType.FullName}\", dependency \"{targetType.Name}\" was not found."); From 4274900d431e1316738a9e34bac76f667a8a123a Mon Sep 17 00:00:00 2001 From: Khionu Sybiern Date: Wed, 1 Mar 2017 14:19:28 -0500 Subject: [PATCH 3/4] Implimented discussed changes --- src/Discord.Net.Commands/Dependencies/DependencyMap.cs | 4 ++++ src/Discord.Net.Commands/Utilities/ReflectionUtils.cs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs index f5adf1a8c..b89ab4370 100644 --- a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs +++ b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs @@ -38,6 +38,8 @@ namespace Discord.Commands public void AddFactory(Func factory) where T : class { var t = typeof(T); + if (typeof(T) == typeof(IDependencyMap) || typeof(T) == typeof(CommandService)) + throw new InvalidOperationException("The dependency map cannot contain services directly added as IDependencyMap or CommandService. Only Implimentations and Derivatives are permitted"); if (map.ContainsKey(t)) throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\""); map.Add(t, factory); @@ -48,6 +50,8 @@ namespace Discord.Commands var t = typeof(T); if (map.ContainsKey(t)) return false; + if (typeof(T) == typeof(IDependencyMap) || typeof(T) == typeof(CommandService)) + throw new InvalidOperationException("The dependency map cannot contain services directly added as IDependencyMap or CommandService. Only Implimentations and Derivatives are permitted"); map.Add(t, factory); return true; } diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs index fdbc8d015..2eaa6a882 100644 --- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs @@ -60,7 +60,7 @@ namespace Discord.Commands { if (targetType == typeof(CommandService)) arg = service; - else if (_dependencyTypeInfo.IsAssignableFrom(targetType.GetTypeInfo())) + else if (targetType == typeof(IDependencyMap) || targetType == map.GetType()) arg = map; else throw new InvalidOperationException($"Failed to create \"{baseType.FullName}\", dependency \"{targetType.Name}\" was not found."); From ba406bb646cb66103a412cb4ecc5ac6aaffeb8e5 Mon Sep 17 00:00:00 2001 From: Khionu Sybiern Date: Thu, 2 Mar 2017 01:39:45 -0500 Subject: [PATCH 4/4] Split typechecks into their own conditions --- .../Dependencies/DependencyMap.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs index b89ab4370..7fb8d33c9 100644 --- a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs +++ b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs @@ -38,8 +38,10 @@ namespace Discord.Commands public void AddFactory(Func factory) where T : class { var t = typeof(T); - if (typeof(T) == typeof(IDependencyMap) || typeof(T) == typeof(CommandService)) - throw new InvalidOperationException("The dependency map cannot contain services directly added as IDependencyMap or CommandService. Only Implimentations and Derivatives are permitted"); + if (typeof(T) == typeof(IDependencyMap)) + throw new InvalidOperationException("IDependencyMap is used internally and cannot be added as a dependency"); + if (typeof(T) == typeof(CommandService)) + throw new InvalidOperationException("CommandService is used internally and cannot be added as a dependency"); if (map.ContainsKey(t)) throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\""); map.Add(t, factory); @@ -50,8 +52,10 @@ namespace Discord.Commands var t = typeof(T); if (map.ContainsKey(t)) return false; - if (typeof(T) == typeof(IDependencyMap) || typeof(T) == typeof(CommandService)) - throw new InvalidOperationException("The dependency map cannot contain services directly added as IDependencyMap or CommandService. Only Implimentations and Derivatives are permitted"); + if (typeof(T) == typeof(IDependencyMap)) + throw new InvalidOperationException("IDependencyMap is used internally and cannot be added as a dependency"); + if (typeof(T) == typeof(CommandService)) + throw new InvalidOperationException("CommandService is used internally and cannot be added as a dependency"); map.Add(t, factory); return true; }