How can I get Method Under One interface like this:
My service classes:
Module A
public class CollectorA : ICollector
{
public string CollectSomething()
{
//Do something
}
}
Module B
public class CollectorB : ICollector
{
public string CollectSomething()
{
//Do something
}
}
Module C
public class CollectorC : ICollector
{
public string CollectSomething()
{
//Do something
}
}
My ICollector interface:
public interface ICollector
{
string CollectSomething();
}
My IModuleInitializer:
public interface IModuleInitializer
{
void Init(IServiceCollection serviceCollection);
}
Under Every Service I have ModuleInitializer like this
public void Init(IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<ICollector, CollectorA>();
serviceCollection.AddTransient<ICollector, CollectorB>();
serviceCollection.AddTransient<ICollector, CollectorC>();
}
How can I access for example in win console application this methods over this interface ?
My main class :
static void Main(string[] args)
{
//Get String Values each module and show on console ..
}
serviceProvider.GetServices<ICollector>()extension method. Note it isGetServices(plural). It would returnIEnumerable<ICollector>containing all the registered implementations.