diff --git a/src/stashbox/Exceptions/ConstructorNotFoundException.cs b/src/stashbox/Exceptions/ConstructorNotFoundException.cs index 436b6c93..86cd1ee3 100644 --- a/src/stashbox/Exceptions/ConstructorNotFoundException.cs +++ b/src/stashbox/Exceptions/ConstructorNotFoundException.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; namespace Stashbox.Exceptions { @@ -14,11 +12,29 @@ public class ConstructorNotFoundException : Exception /// Constructs a . /// /// The type on the constructor was not found. - /// The length of the arguments. + /// The arguments. /// The inner exception - public ConstructorNotFoundException(Type type, int argumentsLength, Exception innerException = null) : - base($"Constructor not found for {type.FullName} with {argumentsLength} arguments.", innerException) - { - } + public ConstructorNotFoundException(Type type, Type[] argumentTypes, Exception innerException = null) : + base($"Constructor not found for {type.FullName} with the given argument types: {argumentTypes.Select(t => t.FullName).Aggregate((t1, t2) => $"{t1}, {t2}")}", innerException) + { } + + /// + /// Constructs a . + /// + /// The type on the constructor was not found. + /// The inner exception + public ConstructorNotFoundException(Type type, Exception innerException = null) : + base($"Constructor not found for {type.FullName} with no arguments.", innerException) + { } + + /// + /// Constructs a . + /// + /// The type on the constructor was not found. + /// The argument type. + /// The inner exception + public ConstructorNotFoundException(Type type, Type argument, Exception innerException = null) : + base($"Constructor not found for {type.FullName} with argument: {argument.FullName}", innerException) + { } } } diff --git a/src/stashbox/Registration/RegistrationContext.cs b/src/stashbox/Registration/RegistrationContext.cs index 45b31b7c..98c019f5 100644 --- a/src/stashbox/Registration/RegistrationContext.cs +++ b/src/stashbox/Registration/RegistrationContext.cs @@ -147,7 +147,7 @@ public IFluentServiceRegistrator WithConstructorByArgumentTypes(params Type[] ar { var constructor = this.ImplementationType.GetConstructor(argumentTypes); if (constructor == null) - throw new ConstructorNotFoundException(this.ImplementationType, argumentTypes.Length); + this.ThrowConstructorNotFoundException(this.ImplementationType, argumentTypes); this.Context.SelectedConstructor = constructor; return this; @@ -155,13 +155,25 @@ public IFluentServiceRegistrator WithConstructorByArgumentTypes(params Type[] ar public IFluentServiceRegistrator WithConstructorByArguments(params object[] arguments) { - var constructor = this.ImplementationType.GetConstructor(arguments.Select(arg => arg.GetType()).ToArray()); + var argTypes = arguments.Select(arg => arg.GetType()).ToArray(); + var constructor = this.ImplementationType.GetConstructor(argTypes); if (constructor == null) - throw new ConstructorNotFoundException(this.ImplementationType, arguments.Length); + this.ThrowConstructorNotFoundException(this.ImplementationType, argTypes); this.Context.SelectedConstructor = constructor; this.Context.ConstructorArguments = arguments; return this; } + + private void ThrowConstructorNotFoundException(Type type, params Type[] argTypes) + { + if (argTypes.Length == 0) + throw new ConstructorNotFoundException(type); + + if (argTypes.Length == 1) + throw new ConstructorNotFoundException(type, argTypes[0]); + + throw new ConstructorNotFoundException(type, argTypes); + } } }