惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 华博

SAP-续1 SAP專欄 也玩玩QQ的WEB(http://user.qzone.qq.com/7355541) 升職感言 - 华博 - 博客园 深深地記憶2007-07-09 重在參預:我眼中的東莞 勞動節到了,你准備好了嗎 四月一號去哪裡玩 新年來,又瘦下來了 Workflow培訓 方言武汉 最近相片,露個臉---SERVER 房間 - 华博 RFID時代來臨了 一切源于基本 关于设计器类程序的模型,先抄下來,慢慢消化 避免死锁 c#中的回車jsp - 华博 - 博客园 Cursor - 华博 spused
System.ComponentModel.Component入门
华博 · 2006-03-22 · via 博客园 - 华博

我们经常看见许多.NET的Class是从System.ComponentModel.Component继承过来的,他实现了System.ComponentModel.IComponent接口,而且MS也在Visual Studio .NET中,推荐你经常使用System.ComponentModel.Component作为基础类。但是很遗憾,很多人不知道为什么这样做。

MS的解释包括:
1、控制外部资源
      IComponent 接口继承自 System.IDisposable 接口,这样可以控制对象的释放。

2、设计时支持
      只要是支持IComponent接口,都可以看见一个设计器,并且拖入到这个组件中的子组件都会自动产生以下代码:

this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);

以便加入站点。

3、承载组件
     所有的子组件都是通过 IContainer 管理的,所以你的子组件都是被管理的。

事实上,很多人看见这些特性还是云里雾里,对于ISite、IContainer和IComponment的关系还是难以理解,那么我们看看这个图形。

通过这个图,我想理解应该好一些吧。

讨论:
1、IComponent使用了注入依赖的思想;
      我发现IComponent的Site属性是Get 和Set的,也就是说,IComponent实现需要有人初始化Site才能正常工作,不信的话你直接实例化一个Component对象,访问Site你会发现是Null的。
      Visual Studio .NET的设计器在实例化一个IComponent对象时,会自动产生如下的代码:

this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);

这段代码实际上就是帮助你的errorProvider1对象初始化Site属性。
       另外,ISite继承了System.IServiceProvider,所以也对应的注入了服务的提供者。
       关于注入依赖,请参考查找AOP和Ioc

2、.NET 2.0 增加了INestedContainer
     容器一般来说,也是被另外一个IComponent持有,你也许注意到我们的设计器总是会生成这样的代码:

private System.ComponentModel.IContainer components = null;

     但是这个容器对象,我们发现他并不包含 Owner 的引用,也就是说,你的子对象可以互相通讯,但是你的子对象不能访问到 他的父。
     基于以上问题,.NET 2.0中增加了INestedContainer接口(继承自IContainer),他新增了Owner属性,可以访问到容器所在的所有者,即父。

3、IComponent适合的范围
      我注意到,自动生成的代码中,这个组件公用一个容器,也就是说,只有整个组件释放,容器才会释放。所以当你使用一个组件时,请及时的释放这个对象。
      如果你打算设计无状态的服务类,请不要使用设计器,而是需要某个子组件时,才创建,并使用容器统一管理,诸如以下的代码: