Skip to content

Commit 4c1509b

Browse files
authored
Merge pull request grumpydev#40 from richardhopton/Tests/UsingConstructorFromAnotherType
Add check for incorrect constructor used
2 parents c648382 + 97b3b24 commit 4c1509b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/TinyIoC.Tests/TinyIoCFunctionalTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public void NestedClassDependencies_CorrectlyRegistered_ResolvesRoot()
8080
Assert.IsInstanceOfType(result, typeof(NestedClassDependencies.RootClass));
8181
}
8282

83+
[TestMethod]
84+
public void NestedClassDependencies_UsingConstructorFromAnotherType_ThrowsException()
85+
{
86+
var container = UtilityMethods.GetContainer();
87+
var registerOptions = container.Register<NestedClassDependencies.RootClass>();
88+
89+
AssertHelper.ThrowsException<TinyIoCConstructorResolutionException>
90+
(() => registerOptions.UsingConstructor(() => new RootClass(null, null)));
91+
}
92+
8393
[TestMethod]
8494
public void NestedClassDependencies_MissingService3Registration_ResolvesRootResolutionOn()
8595
{

src/TinyIoC/TinyIoC.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,9 @@ public RegisterOptions WithStrongReference()
12031203
#if EXPRESSIONS
12041204
public RegisterOptions UsingConstructor<RegisterType>(Expression<Func<RegisterType>> constructor)
12051205
{
1206+
if(!IsValidAssignment(_Registration.Type, typeof(RegisterType)))
1207+
throw new TinyIoCConstructorResolutionException(typeof(RegisterType));
1208+
12061209
var lambda = constructor as LambdaExpression;
12071210
if (lambda == null)
12081211
throw new TinyIoCConstructorResolutionException(typeof(RegisterType));

0 commit comments

Comments
 (0)