- fix invalidOperationException when trying to resolve a type, that is registered as an open generic, as a parameter

master
Simon G. 12 months ago
parent 66a1d22941
commit f530cc1301
Signed by: SimonG
GPG Key ID: 0B82B964BA536523
  1. 16
      LightweightIocContainer/IocContainer.cs
  2. 22
      Test.LightweightIocContainer/OpenGenericRegistrationTest.cs

@ -231,10 +231,18 @@ public class IocContainer : IIocContainer, IIocResolver
parametersToResolve ??= [];
parametersToResolve.Insert(0, multitonScopeArgument); //insert scope at first place, won't be passed to ctor when creating multiton
}
if (result)
return (true, new InternalToBeResolvedPlaceholder(registeredType, registration, parametersToResolve), null);
switch (result)
{
case true when registration is IOpenGenericRegistration openGenericRegistration:
{
Type genericImplementationType = openGenericRegistration.ImplementationType.MakeGenericType(typeof(T).GenericTypeArguments);
return (true, new InternalToBeResolvedPlaceholder(genericImplementationType, registration, parametersToResolve), null);
}
case true:
return (true, new InternalToBeResolvedPlaceholder(registeredType, registration, parametersToResolve), null);
}
if (exception != null)
return (false, new object(), exception);

@ -49,6 +49,18 @@ public class OpenGenericRegistrationTest
ITest<T> Create<T>(T item) where T : IConstraint, new();
}
[UsedImplicitly]
public interface IA;
[UsedImplicitly]
public class A : IA
{
public A(ITest<Constraint> test)
{
}
}
[SetUp]
public void SetUp() => _iocContainer = new IocContainer();
@ -104,4 +116,14 @@ public class OpenGenericRegistrationTest
ITest<Constraint> test = testFactory.Create(new Constraint());
Assert.That(test, Is.InstanceOf<CtorTest<Constraint>>());
}
[Test]
public void TestOpenGenericTypeAsParameter()
{
_iocContainer.Register(r => r.Add<IA, A>());
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>), Lifestyle.Singleton));
IA a = _iocContainer.Resolve<IA>();
Assert.That(a, Is.TypeOf<A>());
}
}
Loading…
Cancel
Save