Files
Discord.Net/docs/guides/dependency_injection/samples/implicit-registration.cs
2022-08-02 11:20:27 +02:00

13 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);
}