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

推荐订阅源

T
Threatpost
O
OpenAI News
Forbes - Security
Forbes - Security
W
WeLiveSecurity
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
P
Privacy & Cybersecurity Law Blog
The Register - Security
The Register - Security
T
Tor Project blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
Secure Thoughts
D
DataBreaches.Net
Vercel News
Vercel News
D
Docker
T
The Blog of Author Tim Ferriss
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
博客园_首页
S
Schneier on Security
宝玉的分享
宝玉的分享
Project Zero
Project Zero
V
Visual Studio Blog
Attack and Defense Labs
Attack and Defense Labs
量子位
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
B
Blog
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
N
Netflix TechBlog - Medium
H
Hacker News: Front Page
Cloudbric
Cloudbric
云风的 BLOG
云风的 BLOG
Latest news
Latest news
P
Proofpoint News Feed
Help Net Security
Help Net Security
Schneier on Security
Schneier on Security
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
有赞技术团队
有赞技术团队
B
Blog RSS Feed
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog

博客园 - 华博

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