Discord.Net/docs/guides/dependency_injection/samples/implicit-registration.cs
2024-06-13 17:43:59 +12:00

12 lines
549 B
C#

public static ServiceCollection RegisterImplicitServices(this ServiceCollection collection, Type interfaceType, Type activatorType)
{
// Get all types in the executing assembly. There are many ways to do this, but this is fastest.
foreach (var type in typeof(Program).Assembly.GetTypes())
{
if (interfaceType.IsAssignableFrom(type) && !type.IsAbstract)
collection.AddSingleton(interfaceType, type);
}
// Register the activator so you can activate the instances.
collection.AddSingleton(activatorType);
}