From 39d8e3c159606757cd6e58793ff6e06fb78167de Mon Sep 17 00:00:00 2001 From: Finite Reality Date: Thu, 21 Jul 2016 01:44:14 +0100 Subject: [PATCH] Allow users to get IDependencyMap if they follow a strict format --- src/Discord.Net.Commands/ReflectionUtils.cs | 45 ++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/Discord.Net.Commands/ReflectionUtils.cs b/src/Discord.Net.Commands/ReflectionUtils.cs index 4603563d8..dab5dfda8 100644 --- a/src/Discord.Net.Commands/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/ReflectionUtils.cs @@ -17,25 +17,50 @@ namespace Discord.Commands if (constructor == null) throw new InvalidOperationException($"Found no constructor for \"{typeInfo.FullName}\""); - object[] parameters; - try + object[] arguments = null; + + ParameterInfo[] parameters = constructor.GetParameters(); + + // TODO: can this logic be made better/cleaner? + if (parameters.Length == 1) { - // TODO: probably change this ternary into something sensible - parameters = constructor.GetParameters() - .Select(x => x.ParameterType == typeof(CommandService) ? service : map.Get(x.ParameterType)).ToArray(); + if (parameters[0].ParameterType == typeof(IDependencyMap)) + { + if (map != null) + arguments = new object[] { map }; + else + throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)"); + } } - catch (KeyNotFoundException ex) // tried to inject an invalid dependency + else if (parameters.Length == 2) { - throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (could not provide parameter)", ex); + if (parameters[0].ParameterType == typeof(CommandService) && parameters[1].ParameterType == typeof(IDependencyMap)) + if (map != null) + arguments = new object[] { service, map }; + else + throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)"); } - catch (NullReferenceException ex) // tried to find a dependency + + if (arguments == null) { - throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (type requires dependency injection)", ex); + try + { + // TODO: probably change this ternary into something sensible? + arguments = parameters.Select(x => x.ParameterType == typeof(CommandService) ? service : map.Get(x.ParameterType)).ToArray(); + } + catch (KeyNotFoundException ex) // tried to inject an invalid dependency + { + throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (could not provide parameter)", ex); + } + catch (NullReferenceException ex) // tried to find a dependency + { + throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)", ex); + } } try { - return constructor.Invoke(parameters); + return constructor.Invoke(arguments); } catch (Exception ex) {