Last active
September 16, 2015 10:29
-
-
Save blazey/74a8eaad16641417e34b to your computer and use it in GitHub Desktop.
Install features by namespace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class InstallFeature<TInSameNamespaceAs> : IWindsorInstaller | |
{ | |
public void Install(IWindsorContainer container, IConfigurationStore store) | |
{ | |
container.Register(Classes.FromThisAssembly() | |
.InSameNamespaceAs<TInSameNamespaceAs>() | |
.Unless(type => type.IsAssignableToGenericType(typeof (IEventHandler<>))) | |
.Unless(type => type.IsAssignableToGenericType(typeof (IResourceCommandHandler<>)) | |
.Unless(type => typeof (Now).IsAssignableFrom(type)) | |
.Unless(type => typeof (IWindsorInstaller).IsAssignableFrom(type)) | |
.WithService.DefaultInterfaces()); | |
.WithService.DefaultInterfaces()); | |
} | |
private static bool IsAssignableToGenericType(Type givenType, Type genericType) | |
{ | |
if (givenType == null || genericType == null) | |
{ | |
return false; | |
} | |
return givenType == genericType | |
|| MapsToGenericTypeDefinition(givenType, genericType) | |
|| HasInterfaceThatMapsToGenericTypeDefinition(givenType, genericType) | |
|| IsAssignableToGenericType(givenType.BaseType, genericType); | |
} | |
private static bool HasInterfaceThatMapsToGenericTypeDefinition(Type givenType, Type genericType) | |
{ | |
return givenType | |
.GetInterfaces() | |
.Where(it => it.IsGenericType) | |
.Any(it => it.GetGenericTypeDefinition() == genericType); | |
} | |
private static bool MapsToGenericTypeDefinition(Type givenType, Type genericType) | |
{ | |
return genericType.IsGenericTypeDefinition | |
&& givenType.IsGenericType | |
&& givenType.GetGenericTypeDefinition() == genericType; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment