From cb4ea88597e6b842e6251a0d0d92420c89dd8f04 Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Thu, 17 Oct 2019 15:08:20 +0200 Subject: [PATCH] - add boxing for simple datatypes in factory creation - add test for factories with simple datatypes as parameters --- .../Registrations/TypedFactoryRegistration.cs | 1 + .../IocContainerTest.cs | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs b/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs index dda3eab..4bd8bc2 100644 --- a/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs +++ b/LightweightIocContainer/Registrations/TypedFactoryRegistration.cs @@ -118,6 +118,7 @@ namespace LightweightIocContainer.Registrations generator.Emit(OpCodes.Dup); generator.Emit(OpCodes.Ldc_I4_S, i); generator.Emit(OpCodes.Ldarg_S, i + 1); + generator.Emit(OpCodes.Box, args[i].ParameterType); //Boxing is only needed for simple datatypes, but for now it is not a problem to box everything generator.Emit(OpCodes.Stelem_Ref); } } diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index a20e2eb..d690d25 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -26,6 +26,7 @@ namespace Test.LightweightIocContainer ITest Create(string name); ITest Create(MultitonScope scope); ITest CreateTest(string name = null); + ITest Create(byte id); void ClearMultitonInstance(); } @@ -61,6 +62,17 @@ namespace Test.LightweightIocContainer } } + [UsedImplicitly] + private class TestByte : ITest + { + private readonly byte _id; + + public TestByte(byte id) + { + _id = id; + } + } + public class MultitonScope { @@ -346,6 +358,18 @@ namespace Test.LightweightIocContainer Assert.IsInstanceOf(createdTest); } + [Test] + public void TestResolveFromFactoryWithByte() + { + _iocContainer.Register(); + _iocContainer.RegisterFactory(); + + ITestFactory testFactory = _iocContainer.Resolve(); + ITest createdTest = testFactory.Create(1); + + Assert.IsInstanceOf(createdTest); + } + [Test] public void TestResolveMultitonFromFactory() {