6

我是一名新测试人员,在阅读遗留代码时,我有以下两个类:

public class TestCommon : Component
{
    public void Initialize()
    {
        var serviceContainer = (IServiceContainer)this.GetService(typeof(TestFramework));
        serviceContainer.AddService(typeof(TestCommon), this);
    }
}

public class TestFramework : ISite, IServiceContainer
{
    readonly Hashtable services = new Hashtable();
    public TestFramework()
    {
        this.AddService(this);

        var bedrockModuleInstance = (TestCommon)Activator.CreateInstance(typeof(TestCommon));
        ((TestCommon)bedrockModuleInstance).Site = this;
        ((TestCommon)bedrockModuleInstance).Initialize();
    }
}

我不明白为什么在TestCommon 类的Initialize 方法中,可以调用GetService 并以某种方式返回调用TestFramework 的GetService?我试图通过阅读有关容器、组件和站点的 MSDN 来理解它,但无法理解站点的概念。

更新: 阅读 GetService 的实现,发现组件的 GetService 实际返回其站点的 GetService,回答了我的问题。

   protected virtual object GetService(Type service) { 
        ISite s = site;
        return((s== null) ? null : s.GetService(service)); 
    } 
4

1 回答 1

3

找到了答案。阅读 GetService 的实现,发现组件的 GetService 实际返回其站点的 GetService,回答了我的问题。

protected virtual object GetService(Type service) { 
    ISite s = site;
    return((s== null) ? null : s.GetService(service)); 
} 
于 2012-12-19T01:59:16.827 回答