C# Asp.net核心在一个接口下获取方法

C# Asp.net核心在一个接口下获取方法,c#,asp.net-core,C#,Asp.net Core,如何在这样一个接口下获取方法: 我的服务类别: public interface ICollector { string CollectSomething(); } public interface IModuleInitializer { void Init(IServiceCollection serviceCollection); } 模块A public class CollectorA : ICollector { public string Collect

如何在这样一个接口下获取方法:

我的服务类别:

public interface ICollector
{
    string CollectSomething();
}
public interface IModuleInitializer
{
    void Init(IServiceCollection serviceCollection);
}
模块A

public class CollectorA : ICollector
{
    public string CollectSomething()
    {
        //Do something
    }
}
模块B

public class CollectorB : ICollector
{
    public string CollectSomething()
    {
        //Do something
    }
}
模块C

public class CollectorC : ICollector
{
    public string CollectSomething()
    {
        //Do something
    }
}
我的ICollector接口:

public interface ICollector
{
    string CollectSomething();
}
public interface IModuleInitializer
{
    void Init(IServiceCollection serviceCollection);
}
我的初始值设定项:

public interface ICollector
{
    string CollectSomething();
}
public interface IModuleInitializer
{
    void Init(IServiceCollection serviceCollection);
}
在每个服务下,我都有这样的模块初始化器

public void Init(IServiceCollection serviceCollection)
{
    serviceCollection.AddTransient<ICollector, CollectorA>();
    serviceCollection.AddTransient<ICollector, CollectorB>();
    serviceCollection.AddTransient<ICollector, CollectorC>();
}

假设以下实现

pubic class ModuleInitializer : IModuleInitializer{
    public void Init(IServiceCollection serviceCollection) {
        serviceCollection.AddTransient<ICollector, CollectorA>();
        serviceCollection.AddTransient<ICollector, CollectorB>();
        serviceCollection.AddTransient<ICollector, CollectorC>();
    }
}

构建服务提供程序后,可以使用
serviceProvider.GetServices()
extension方法。注意它是
GetServices
(复数)。它将返回包含所有注册实现的
IEnumerable
。您能在这里共享一些代码吗@恩科西对你想要什么做了一个假设,但你的问题仍然有点不清楚。你到底想做什么?例如我的方法A做两个数的和(2+4),方法B做(3+5)。。。我想在主应用程序中调用它们。我认为更好的实现是使用工厂模式或策略模式。没有问题了:)所有功能都正常,但我正在尝试添加.Transient();自动地简而言之,我不想在每个模块实现中都使用addTransient。需要帮忙吗谢谢