#26: add noMatchingConstructorFoundException:

- throw detailed exception when no matching constructor is found
- add innerExceptions with information about the constructors trying to be used
pull/32/head
Simon Gockner 6 years ago
parent 981dd219ad
commit ffbce37561
  1. 31
      LightweightIocContainer/Exceptions/ConstructorNotMatchingException.cs
  2. 53
      LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs
  3. 14
      LightweightIocContainer/IocContainer.cs
  4. 46
      LightweightIocContainer/LightweightIocContainer.xml

@ -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
{
/// <summary>
/// The constructor does not match the given or resolvable arguments
/// </summary>
internal class ConstructorNotMatchingException : Exception
{
/// <summary>
/// The constructor does not match the given or resolvable arguments
/// </summary>
/// <param name="constructor">The constructor that does not match</param>
/// <param name="exception">The inner exception</param>
public ConstructorNotMatchingException(ConstructorInfo constructor, Exception exception)
: base($"Constructor {constructor} does not match the given or resolvable arguments.", exception)
{
Constructor = constructor;
}
/// <summary>
/// The constructor that does not match
/// </summary>
public ConstructorInfo Constructor { get; }
}
}

@ -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
{
/// <summary>
/// No matching constructor was found for the given or resolvable arguments
/// </summary>
internal class NoMatchingConstructorFoundException : AggregateException
{
/// <summary>
/// No matching constructor was found for the given or resolvable arguments
/// </summary>
/// <param name="type">The <see cref="Type"/> with no matching constructor</param>
/// <param name="exceptions">The inner exceptions of type <see cref="ConstructorNotMatchingException"/></param>
public NoMatchingConstructorFoundException(Type type, params ConstructorNotMatchingException[] exceptions)
: base($"No matching constructor for {type} found.")
{
Type = type;
if (exceptions == null)
InnerExceptions = new List<ConstructorNotMatchingException>();
else
InnerExceptions = exceptions.ToList();
}
/// <summary>
/// The <see cref="Type"/> with no matching constructor
/// </summary>
public Type Type { get; }
/// <summary>
/// The inner exceptions of the <see cref="NoMatchingConstructorFoundException"/>
/// </summary>
public new List<ConstructorNotMatchingException> InnerExceptions { get; }
/// <summary>
/// Add an inner exception to the <see cref="InnerExceptions"/>
/// </summary>
/// <param name="exception">The <see cref="ConstructorNotMatchingException"/></param>
public void AddInnerException(ConstructorNotMatchingException exception)
{
InnerExceptions.Add(exception);
}
}
}

@ -295,11 +295,15 @@ namespace LightweightIocContainer
/// <param name="type">The <see cref="Type"/> that will be created</param> /// <param name="type">The <see cref="Type"/> that will be created</param>
/// <param name="arguments">The existing arguments</param> /// <param name="arguments">The existing arguments</param>
/// <returns>An array of all needed constructor arguments to create the <see cref="Type"/></returns> /// <returns>An array of all needed constructor arguments to create the <see cref="Type"/></returns>
/// <exception cref="NoMatchingConstructorFoundException">No matching constructor was found for the given or resolvable arguments</exception>
[CanBeNull] [CanBeNull]
private object[] ResolveConstructorArguments(Type type, object[] arguments) private object[] ResolveConstructorArguments(Type type, object[] arguments)
{ {
//find best ctor //find best ctor
IOrderedEnumerable<ConstructorInfo> sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length); IOrderedEnumerable<ConstructorInfo> sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length);
NoMatchingConstructorFoundException noMatchingConstructorFoundException = null;
foreach (ConstructorInfo ctor in sortedConstructors) foreach (ConstructorInfo ctor in sortedConstructors)
{ {
try try
@ -348,11 +352,17 @@ namespace LightweightIocContainer
return ctorParams.ToArray(); 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; return null;
} }

@ -33,6 +33,23 @@
<param name="predicate">A function to test each element for a condition</param> <param name="predicate">A function to test each element for a condition</param>
<returns>The first element of the <see cref="T:System.Collections.Generic.IEnumerable`1"/> or a new instance of the given <see cref="T:System.Type"/> when no element is found</returns> <returns>The first element of the <see cref="T:System.Collections.Generic.IEnumerable`1"/> or a new instance of the given <see cref="T:System.Type"/> when no element is found</returns>
</member> </member>
<member name="T:LightweightIocContainer.Exceptions.ConstructorNotMatchingException">
<summary>
The constructor does not match the given or resolvable arguments
</summary>
</member>
<member name="M:LightweightIocContainer.Exceptions.ConstructorNotMatchingException.#ctor(System.Reflection.ConstructorInfo,System.Exception)">
<summary>
The constructor does not match the given or resolvable arguments
</summary>
<param name="constructor">The constructor that does not match</param>
<param name="exception">The inner exception</param>
</member>
<member name="P:LightweightIocContainer.Exceptions.ConstructorNotMatchingException.Constructor">
<summary>
The constructor that does not match
</summary>
</member>
<member name="T:LightweightIocContainer.Exceptions.IllegalAbstractMethodCreationException"> <member name="T:LightweightIocContainer.Exceptions.IllegalAbstractMethodCreationException">
<summary> <summary>
The creation of the abstract method is illegal in its current state The creation of the abstract method is illegal in its current state
@ -116,6 +133,34 @@
The <see cref="T:System.Type"/> of the multiton that's responsible for the exception The <see cref="T:System.Type"/> of the multiton that's responsible for the exception
</summary> </summary>
</member> </member>
<member name="T:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException">
<summary>
No matching constructor was found for the given or resolvable arguments
</summary>
</member>
<member name="M:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.#ctor(System.Type,LightweightIocContainer.Exceptions.ConstructorNotMatchingException[])">
<summary>
No matching constructor was found for the given or resolvable arguments
</summary>
<param name="type">The <see cref="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.Type"/> with no matching constructor</param>
<param name="exceptions">The inner exceptions of type <see cref="T:LightweightIocContainer.Exceptions.ConstructorNotMatchingException"/></param>
</member>
<member name="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.Type">
<summary>
The <see cref="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.Type"/> with no matching constructor
</summary>
</member>
<member name="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.InnerExceptions">
<summary>
The inner exceptions of the <see cref="T:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException"/>
</summary>
</member>
<member name="M:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.AddInnerException(LightweightIocContainer.Exceptions.ConstructorNotMatchingException)">
<summary>
Add an inner exception to the <see cref="P:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException.InnerExceptions"/>
</summary>
<param name="exception">The <see cref="T:LightweightIocContainer.Exceptions.ConstructorNotMatchingException"/></param>
</member>
<member name="T:LightweightIocContainer.Exceptions.TypeNotRegisteredException"> <member name="T:LightweightIocContainer.Exceptions.TypeNotRegisteredException">
<summary> <summary>
The <see cref="T:System.Type"/> is not registered in this <see cref="T:LightweightIocContainer.Interfaces.IIocContainer"/> The <see cref="T:System.Type"/> is not registered in this <see cref="T:LightweightIocContainer.Interfaces.IIocContainer"/>
@ -528,6 +573,7 @@
<param name="type">The <see cref="T:System.Type"/> that will be created</param> <param name="type">The <see cref="T:System.Type"/> that will be created</param>
<param name="arguments">The existing arguments</param> <param name="arguments">The existing arguments</param>
<returns>An array of all needed constructor arguments to create the <see cref="T:System.Type"/></returns> <returns>An array of all needed constructor arguments to create the <see cref="T:System.Type"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.NoMatchingConstructorFoundException">No matching constructor was found for the given or resolvable arguments</exception>
</member> </member>
<member name="M:LightweightIocContainer.IocContainer.ClearMultitonInstances``1"> <member name="M:LightweightIocContainer.IocContainer.ClearMultitonInstances``1">
<summary> <summary>

Loading…
Cancel
Save