diff --git a/LightweightIocContainer/Exceptions/NoPublicConstructorFoundException.cs b/LightweightIocContainer/Exceptions/NoPublicConstructorFoundException.cs new file mode 100644 index 0000000..818a856 --- /dev/null +++ b/LightweightIocContainer/Exceptions/NoPublicConstructorFoundException.cs @@ -0,0 +1,29 @@ +// // Author: Simon Gockner +// // Created: 2019-11-04 +// // Copyright(c) 2019 SimonG. All Rights Reserved. + +using System; + +namespace LightweightIocContainer.Exceptions +{ + /// + /// No public constructor can be found for a + /// + internal class NoPublicConstructorFoundException : Exception + { + /// + /// No public constructor can be found for a + /// + /// The with no public constructor + public NoPublicConstructorFoundException(Type type) + : base($"No public constructor for {type} found.") + { + Type = type; + } + + /// + /// The with no public constructor + /// + public Type Type { get; } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index a78d687..9ceee68 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -300,7 +300,9 @@ namespace LightweightIocContainer private object[] ResolveConstructorArguments(Type type, object[] arguments) { //find best ctor - IOrderedEnumerable sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length); + List sortedConstructors = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).ToList(); + if (!sortedConstructors.Any()) //no public constructor available + throw new NoPublicConstructorFoundException(type); NoMatchingConstructorFoundException noMatchingConstructorFoundException = null; diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index ae70c67..69c7a7d 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -161,6 +161,22 @@ The + + + No public constructor can be found for a + + + + + No public constructor can be found for a + + The with no public constructor + + + + The with no public constructor + + The is not registered in this diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index 23ed584..d667c7c 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -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(() => _iocContainer.Resolve()); } + [Test] + public void TestResolvePrivateConstructor() + { + _iocContainer.Register(); + Assert.Throws(() => _iocContainer.Resolve()); + } + [Test] public void TestResolveFactory() {