6
我是一个新的测试,并在阅读遗留代码,我有以下两类:什么是.net中“组件”的“网站”?
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的初始化方法,一个可以打电话的GetService并返回莫名其妙的TestFramework”的GetService被调用?我试着通过阅读有关Container,Component和Site的MSDN了解它,但无法理解Site的想法。
更新: 阅读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。 –