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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
K
Kaspersky official blog
S
Schneier on Security
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
The Register - Security
The Register - Security
T
Threat Research - Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
H
Help Net Security
博客园_首页
I
Intezer
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Project Zero
Project Zero
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
Vercel News
Vercel News
A
Arctic Wolf
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
P
Privacy International News Feed
G
GRAHAM CLULEY
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
Simon Willison's Weblog
Simon Willison's Weblog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
T
The Exploit Database - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Latest
Security Latest
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News 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适合的范围
      我注意到,自动生成的代码中,这个组件公用一个容器,也就是说,只有整个组件释放,容器才会释放。所以当你使用一个组件时,请及时的释放这个对象。
      如果你打算设计无状态的服务类,请不要使用设计器,而是需要某个子组件时,才创建,并使用容器统一管理,诸如以下的代码: