From 0b2879ddc3f2534cf5189ca2ffa4da3706ef79a3 Mon Sep 17 00:00:00 2001 From: Simon G Date: Fri, 18 Sep 2020 22:38:19 +0200 Subject: [PATCH] - add registration methods for OpenGenericRegistration --- .../Interfaces/IIocContainer.cs | 3 +++ LightweightIocContainer/IocContainer.cs | 27 +++++++++---------- .../Registrations/RegistrationFactory.cs | 5 ++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/LightweightIocContainer/Interfaces/IIocContainer.cs b/LightweightIocContainer/Interfaces/IIocContainer.cs index 62a4434..fe0f94a 100644 --- a/LightweightIocContainer/Interfaces/IIocContainer.cs +++ b/LightweightIocContainer/Interfaces/IIocContainer.cs @@ -29,6 +29,9 @@ namespace LightweightIocContainer.Interfaces /// The created IDefaultRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface; + IOpenGenericRegistration Register(Type tInterface, Type tImplementation, + Lifestyle lifestyle = Lifestyle.Transient); + /// /// Register multiple interfaces for a that implements them /// diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 6f114be..bf5f1a2 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -68,6 +68,14 @@ namespace LightweightIocContainer return registration; } + public IOpenGenericRegistration Register(Type tInterface, Type tImplementation, Lifestyle lifestyle = Lifestyle.Transient) + { + IOpenGenericRegistration registration = _registrationFactory.Register(tInterface, tImplementation, lifestyle); + Register(registration); + + return registration; + } + /// /// Register multiple interfaces for a that implements them /// @@ -246,20 +254,7 @@ namespace LightweightIocContainer /// Could not find function private object Resolve(Type type, object[] arguments, List resolveStack) { - MethodInfo resolveMethod = typeof(IocContainer).GetMethod(nameof(ResolveInternal), BindingFlags.NonPublic | BindingFlags.Instance); - MethodInfo genericResolveMethod = resolveMethod?.MakeGenericMethod(type); - - if (genericResolveMethod == null) - throw new InternalResolveException($"Could not find function {nameof(ResolveInternal)}"); - - try //exceptions thrown by methods called with invoke are wrapped into another exception, the exception thrown by the invoked method can be returned by `Exception.GetBaseException()` - { - return genericResolveMethod.Invoke(this, new object[] { arguments, resolveStack }); - } - catch (Exception ex) - { - throw ex.GetBaseException(); - } + return GenericMethodCaller.Call(this, nameof(ResolveInternal), type, BindingFlags.NonPublic | BindingFlags.Instance, arguments, resolveStack); } /// @@ -299,6 +294,10 @@ namespace LightweightIocContainer else if (registration is ITypedFactoryRegistration typedFactoryRegistration) { resolvedInstance = typedFactoryRegistration.Factory.Factory; + } + else if (registration is IOpenGenericRegistration openGenericRegistration) + { + } else throw new UnknownRegistrationException($"There is no registration of type {registration.GetType().Name}."); diff --git a/LightweightIocContainer/Registrations/RegistrationFactory.cs b/LightweightIocContainer/Registrations/RegistrationFactory.cs index f87383c..7ff2487 100644 --- a/LightweightIocContainer/Registrations/RegistrationFactory.cs +++ b/LightweightIocContainer/Registrations/RegistrationFactory.cs @@ -33,6 +33,11 @@ namespace LightweightIocContainer.Registrations return new DefaultRegistration(typeof(TInterface), typeof(TImplementation), lifestyle); } + public IOpenGenericRegistration Register(Type tInterface, Type tImplementation, Lifestyle lifestyle) + { + return new OpenGenericRegistration(tInterface, tImplementation, lifestyle); + } + /// /// Register multiple interfaces for a that implements them and create a ///