什么是.net中"组件"的"站点"?

zha*_*nwu 6 .net c#

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

   protected virtual object GetService(Type service) { 
        ISite s = site;
        return((s== null) ? null : s.GetService(service)); 
    } 
Run Code Online (Sandbox Code Playgroud)

zha*_*nwu 3

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

protected virtual object GetService(Type service) { 
    ISite s = site;
    return((s== null) ? null : s.GetService(service)); 
} 
Run Code Online (Sandbox Code Playgroud)