From ffbce375612014edb2a04c4ef87d9b2173147213 Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Mon, 4 Nov 2019 16:33:16 +0100 Subject: [PATCH] #26: add noMatchingConstructorFoundException: - throw detailed exception when no matching constructor is found - add innerExceptions with information about the constructors trying to be used --- .../ConstructorNotMatchingException.cs | 31 +++++++++++ .../NoMatchingConstructorFoundException.cs | 53 +++++++++++++++++++ LightweightIocContainer/IocContainer.cs | 14 ++++- .../LightweightIocContainer.xml | 46 ++++++++++++++++ 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 LightweightIocContainer/Exceptions/ConstructorNotMatchingException.cs create mode 100644 LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs diff --git a/LightweightIocContainer/Exceptions/ConstructorNotMatchingException.cs b/LightweightIocContainer/Exceptions/ConstructorNotMatchingException.cs new file mode 100644 index 0000000..8d94ae4 --- /dev/null +++ b/LightweightIocContainer/Exceptions/ConstructorNotMatchingException.cs @@ -0,0 +1,31 @@ +// Author: Gockner, Simon +// Created: 2019-11-04 +// Copyright(c) 2019 SimonG. All Rights Reserved. + +using System; +using System.Reflection; + +namespace LightweightIocContainer.Exceptions +{ + /// + /// The constructor does not match the given or resolvable arguments + /// + internal class ConstructorNotMatchingException : Exception + { + /// + /// The constructor does not match the given or resolvable arguments + /// + /// The constructor that does not match + /// The inner exception + public ConstructorNotMatchingException(ConstructorInfo constructor, Exception exception) + : base($"Constructor {constructor} does not match the given or resolvable arguments.", exception) + { + Constructor = constructor; + } + + /// + /// The constructor that does not match + /// + public ConstructorInfo Constructor { get; } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs b/LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs new file mode 100644 index 0000000..40bab03 --- /dev/null +++ b/LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs @@ -0,0 +1,53 @@ +// Author: Gockner, Simon +// Created: 2019-11-04 +// Copyright(c) 2019 SimonG. All Rights Reserved. + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace LightweightIocContainer.Exceptions +{ + /// + /// No matching constructor was found for the given or resolvable arguments + /// + internal class NoMatchingConstructorFoundException : AggregateException + { + /// + /// No matching constructor was found for the given or resolvable arguments + /// + /// The with no matching constructor + /// The inner exceptions of type + public NoMatchingConstructorFoundException(Type type, params ConstructorNotMatchingException[] exceptions) + : base($"No matching constructor for {type} found.") + { + Type = type; + + if (exceptions == null) + InnerExceptions = new List(); + else + InnerExceptions = exceptions.ToList(); + } + + + /// + /// The with no matching constructor + /// + public Type Type { get; } + + /// + /// The inner exceptions of the + /// + public new List InnerExceptions { get; } + + + /// + /// Add an inner exception to the + /// + /// The + public void AddInnerException(ConstructorNotMatchingException exception) + { + InnerExceptions.Add(exception); + } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index d1320f4..a78d687 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -295,11 +295,15 @@ namespace LightweightIocContainer /// The that will be created /// The existing arguments /// An array of all needed constructor arguments to create the + /// No matching constructor was found for the given or resolvable arguments [CanBeNull] private object[] ResolveConstructorArguments(Type type, object[] arguments) { //find best ctor IOrderedEnumerable sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length); + + NoMatchingConstructorFoundException noMatchingConstructorFoundException = null; + foreach (ConstructorInfo ctor in sortedConstructors) { try @@ -348,11 +352,17 @@ namespace LightweightIocContainer return ctorParams.ToArray(); } - catch (Exception ex) //TODO: Decide what exactly to do in this case + catch (Exception ex) { - continue; + if (noMatchingConstructorFoundException == null) + noMatchingConstructorFoundException = new NoMatchingConstructorFoundException(type); + + noMatchingConstructorFoundException.AddInnerException(new ConstructorNotMatchingException(ctor, ex)); } } + + if (noMatchingConstructorFoundException != null) + throw noMatchingConstructorFoundException; return null; } diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index 4b01ad3..ae70c67 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -33,6 +33,23 @@ A function to test each element for a condition The first element of the or a new instance of the given when no element is found + + + The constructor does not match the given or resolvable arguments + + + + + The constructor does not match the given or resolvable arguments + + The constructor that does not match + The inner exception + + + + The constructor that does not match + + The creation of the abstract method is illegal in its current state @@ -116,6 +133,34 @@ The of the multiton that's responsible for the exception + + + No matching constructor was found for the given or resolvable arguments + + + + + No matching constructor was found for the given or resolvable arguments + + The with no matching constructor + The inner exceptions of type + + + + The with no matching constructor + + + + + The inner exceptions of the + + + + + Add an inner exception to the + + The + The is not registered in this @@ -528,6 +573,7 @@ The that will be created The existing arguments An array of all needed constructor arguments to create the + No matching constructor was found for the given or resolvable arguments