Skip to content

Instantly share code, notes, and snippets.

@blazey
Last active September 16, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blazey/74a8eaad16641417e34b to your computer and use it in GitHub Desktop.
Save blazey/74a8eaad16641417e34b to your computer and use it in GitHub Desktop.
Install features by namespace
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