Skip to content

Commit

Permalink
Merge pull request #555 from robertcoltheart/refactor/remove-reflection
Browse files Browse the repository at this point in the history
Use generic registration instead of reflection
  • Loading branch information
jonorossi committed Oct 8, 2020
2 parents 5e4416d + a229be4 commit 6ddf7c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

namespace Castle.Windsor.Extensions.DependencyInjection.Extensions
{
using System.Reflection;

using Castle.MicroKernel.Registration;

public static class ServiceDescriptorExtensions
Expand All @@ -26,11 +24,8 @@ public static IRegistration CreateWindsorRegistration(this Microsoft.Extensions.
{
return RegistrationAdapter.FromOpenGenericServiceDescriptor(service);
}
else
{
var method = typeof(RegistrationAdapter).GetMethod("FromServiceDescriptor", BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(service.ServiceType);
return method.Invoke(null, new object[] { service }) as IRegistration;
}

return RegistrationAdapter.FromServiceDescriptor(service);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ public static IRegistration FromOpenGenericServiceDescriptor(Microsoft.Extension
.IsDefault();
}

public static IRegistration FromServiceDescriptor<TService>(Microsoft.Extensions.DependencyInjection.ServiceDescriptor service) where TService : class
public static IRegistration FromServiceDescriptor(Microsoft.Extensions.DependencyInjection.ServiceDescriptor service)
{
var registration = Component.For<TService>()
var registration = Component.For(service.ServiceType)
.NamedAutomatically(UniqueComponentName(service));

if(service.ImplementationFactory != null)
if (service.ImplementationFactory != null)
{
registration = UsingFactoryMethod<TService>(registration, service);
registration = UsingFactoryMethod(registration, service);
}
else if(service.ImplementationInstance != null)
else if (service.ImplementationInstance != null)
{
registration = UsingInstance<TService>(registration, service);
registration = UsingInstance(registration, service);
}
else if(service.ImplementationType != null)
else if (service.ImplementationType != null)
{
registration = UsingImplementation<TService>(registration, service);
registration = UsingImplementation(registration, service);
}

return ResolveLifestyle<TService>(registration, service)
return ResolveLifestyle(registration, service)
.IsDefault();
}

Expand Down

0 comments on commit 6ddf7c9

Please sign in to comment.