diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 7ed4cd6..b4ac730 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -3,7 +3,6 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System.Reflection; -using System.Runtime.CompilerServices; using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Factories; @@ -23,7 +22,7 @@ public class IocContainer : IIocContainer, IIocResolver private readonly RegistrationFactory _registrationFactory; private readonly List<(Type type, object? instance)> _singletons = new(); - private readonly List<(Type type, Type scope, ConditionalWeakTable instances)> _multitons = new(); + private readonly List<(Type type, Type scope, Dictionary instances)> _multitons = new(); private readonly List _ignoreConstructorAttributes; @@ -421,7 +420,7 @@ public class IocContainer : IIocContainer, IIocResolver } T newInstance = Creator.CreateInstance(registration.ImplementationType, arguments[1..]); - _multitons.Add((registration.ImplementationType, registration.Scope, new ConditionalWeakTable { { scopeArgument, newInstance } })); + _multitons.Add((registration.ImplementationType, registration.Scope, new Dictionary { { scopeArgument, newInstance } })); return newInstance; } diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index ebc4a0a..bf42f55 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -34,6 +34,11 @@ public class IocContainerTest } } + + private class TestMultitonIntScope(int scope) : ITest + { + + } [UsedImplicitly] private class TestConstructor : ITest @@ -269,6 +274,20 @@ public class IocContainerTest Assert.AreNotSame(resolvedTest1, resolvedTest3); Assert.AreNotSame(resolvedTest2, resolvedTest3); } + + [Test] + public void TestResolveMultitonIntScope() + { + _iocContainer.Register(r => r.AddMultiton()); + + ITest resolvedTest1 = _iocContainer.Resolve(1); + ITest resolvedTest2 = _iocContainer.Resolve(1); + ITest resolvedTest3 = _iocContainer.Resolve(2); + + Assert.AreSame(resolvedTest1, resolvedTest2); + Assert.AreNotSame(resolvedTest1, resolvedTest3); + Assert.AreNotSame(resolvedTest2, resolvedTest3); + } [Test] public void TestResolveMultitonNoArgs()