From 11ba30c6fa36890c57a98253014eb73d00055cd7 Mon Sep 17 00:00:00 2001 From: RogueException Date: Sat, 18 Mar 2017 08:48:18 -0300 Subject: [PATCH] Cleaned up DepMap type checks --- .../Dependencies/DependencyMap.cs | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs index 7fb8d33c9..c24b9db91 100644 --- a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs +++ b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs @@ -1,10 +1,16 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Discord.Commands { public class DependencyMap : IDependencyMap { + private static readonly Type[] _typeBlacklist = new[] { + typeof(IDependencyMap), + typeof(CommandService) + }; + private Dictionary> map; public static DependencyMap Empty => new DependencyMap(); @@ -37,26 +43,18 @@ namespace Discord.Commands /// public void AddFactory(Func factory) where T : class { - var t = typeof(T); - 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); + if (!TryAddFactory(factory)) + throw new InvalidOperationException($"The dependency map already contains \"{typeof(T).FullName}\""); } /// public bool TryAddFactory(Func factory) where T : class { - var t = typeof(T); - if (map.ContainsKey(t)) + var type = typeof(T); + if (_typeBlacklist.Contains(type)) + throw new InvalidOperationException($"{type.FullName} is used internally and cannot be added as a dependency"); + if (map.ContainsKey(type)) return false; - 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); + map.Add(type, factory); return true; }