从其他项目获取 class 个实例

Getting class instance from other project

我有一个 class 图书馆 class 像这样:

public class GoogleSearch
{
    private IGoogleApiHelper _googleApiHelper;

    public GoogleSearch(IGoogleApiHelper googleApiHelper)
    {
        _googleApiHelper = googleApiHelper;
    }
    //some methods
}

googleApiHelper 是从库的 IoC 容器中获取的服务。 我可以从库外部创建 GoogleSearch class 的实例并使 googleApiHelper 服务从 IoC 容器中获取吗?

googleApiHelper is a service got from IoC container of the library.

阻止这样做。库不应该使用 DI 容器,只有启动项目应该有对 DI 容器的引用,并且应该进行所有必需的注册,包括属于你的库的类型。有关详细信息,请阅读 Composition Root.

如果您遵循 Composition Root 模式,就不会有任何问题,因为一切都在那个地方组合。