在.NET中,"site"和"component"是什么意思?

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)); 
    } 

Site视为组件所有者,例如窗口或表单“拥有”在该窗口中显示的可视组件。有关更多背景信息,请参见http://msdn.microsoft.com/en-us/library/0b1dk63b.aspx。 - Morten Mertner
1个回答

3

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接