Last active
September 8, 2020 15:44
-
-
Save rgueldenpfennig/2c2fa7ae81260ebfd6ad4cfdb284ddf7 to your computer and use it in GitHub Desktop.
Get all types that implement an (generic) interface and register them in .NET Core dependency injection
This file contains hidden or 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
| public static class ServiceCollectionExtensions | |
| { | |
| public static void AddImplementationsOf<T>( | |
| this IServiceCollection services, | |
| Assembly[] assemblies = null, | |
| ServiceLifetime lifetime = ServiceLifetime.Scoped) | |
| { | |
| var serviceType = typeof(T); | |
| services.AddImplementationsOf(serviceType, assemblies, lifetime); | |
| } | |
| public static void AddImplementationsOf( | |
| this IServiceCollection services, | |
| Type serviceType, | |
| Assembly[] assemblies = null, | |
| ServiceLifetime lifetime = ServiceLifetime.Scoped) | |
| { | |
| if (!serviceType.IsInterface) throw new InvalidOperationException(); | |
| if (assemblies == null) assemblies = AppDomain.CurrentDomain.GetAssemblies(); | |
| var implementations = assemblies | |
| .SelectMany(assembly => assembly.DefinedTypes) | |
| .Where(typeInfo => typeInfo.GetInterfaces().Contains(serviceType) && typeInfo.IsClass); | |
| foreach (var implementation in implementations) | |
| { | |
| services.Add(new ServiceDescriptor(serviceType, implementation, lifetime)); | |
| } | |
| } | |
| public static void AddGenericImplementationsOf( | |
| this IServiceCollection services, | |
| Type serviceType, | |
| Assembly[] assemblies = null, | |
| ServiceLifetime lifetime = ServiceLifetime.Scoped) | |
| { | |
| if (assemblies == null) assemblies = AppDomain.CurrentDomain.GetAssemblies(); | |
| var genericType = serviceType; | |
| var implementations = assemblies | |
| .SelectMany(a => a.DefinedTypes.Where(x => x.GetInterfaces() | |
| .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == genericType))); | |
| foreach (var implementation in implementations) | |
| { | |
| var genericInterface = implementation.GetInterface(genericType.Name); | |
| if (genericInterface != null) | |
| { | |
| services.Add(new ServiceDescriptor(genericInterface, implementation, lifetime)); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddImplementationsOf<IMyServiceContract>(lifetime: ServiceLifetime.Transient); | |
| services.AddGenericImplementationsOf(typeof(IValidatableObject<>), new [] { typeof(IValidatableObject<>).Assembly }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment