#27: throw exception when no public constructor is found

pull/32/head
Simon Gockner 6 years ago
parent 15fb9c7d5b
commit 659a45dd96
  1. 29
      LightweightIocContainer/Exceptions/NoPublicConstructorFoundException.cs
  2. 4
      LightweightIocContainer/IocContainer.cs
  3. 16
      LightweightIocContainer/LightweightIocContainer.xml
  4. 16
      Test.LightweightIocContainer/IocContainerTest.cs

@ -0,0 +1,29 @@
// // Author: Simon Gockner
// // Created: 2019-11-04
// // Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
namespace LightweightIocContainer.Exceptions
{
/// <summary>
/// No public constructor can be found for a <see cref="Type"/>
/// </summary>
internal class NoPublicConstructorFoundException : Exception
{
/// <summary>
/// No public constructor can be found for a <see cref="Type"/>
/// </summary>
/// <param name="type">The <see cref="Type"/> with no public constructor</param>
public NoPublicConstructorFoundException(Type type)
: base($"No public constructor for {type} found.")
{
Type = type;
}
/// <summary>
/// The <see cref="Type"/> with no public constructor
/// </summary>
public Type Type { get; }
}
}

@ -300,7 +300,9 @@ namespace LightweightIocContainer
private object[] ResolveConstructorArguments(Type type, object[] arguments)
{
//find best ctor
IOrderedEnumerable<ConstructorInfo> sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length);
List<ConstructorInfo> sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).ToList();
if (!sortedConstructors.Any()) //no public constructor available
throw new NoPublicConstructorFoundException(type);
NoMatchingConstructorFoundException noMatchingConstructorFoundException = null;

@ -161,6 +161,22 @@
</summary>
<param name="exception">The <see cref="T:LightweightIocContainer.Exceptions.ConstructorNotMatchingException"/></param>
</member>
<member name="T:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException">
<summary>
No public constructor can be found for a <see cref="P:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException.Type"/>
</summary>
</member>
<member name="M:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException.#ctor(System.Type)">
<summary>
No public constructor can be found for a <see cref="P:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException.Type"/>
</summary>
<param name="type">The <see cref="P:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException.Type"/> with no public constructor</param>
</member>
<member name="P:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException.Type">
<summary>
The <see cref="P:LightweightIocContainer.Exceptions.NoPublicConstructorFoundException.Type"/> with no public constructor
</summary>
</member>
<member name="T:LightweightIocContainer.Exceptions.TypeNotRegisteredException">
<summary>
The <see cref="T:System.Type"/> is not registered in this <see cref="T:LightweightIocContainer.Interfaces.IIocContainer"/>

@ -62,6 +62,15 @@ namespace Test.LightweightIocContainer
}
}
[UsedImplicitly]
private class TestPrivateConstructor : ITest
{
private TestPrivateConstructor()
{
}
}
[UsedImplicitly]
private class TestByte : ITest
{
@ -303,6 +312,13 @@ namespace Test.LightweightIocContainer
Assert.Throws<NoMatchingConstructorFoundException>(() => _iocContainer.Resolve<ITest>());
}
[Test]
public void TestResolvePrivateConstructor()
{
_iocContainer.Register<ITest, TestPrivateConstructor>();
Assert.Throws<NoPublicConstructorFoundException>(() => _iocContainer.Resolve<ITest>());
}
[Test]
public void TestResolveFactory()
{

Loading…
Cancel
Save