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

推荐订阅源

WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
S
Security @ Cisco Blogs
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
AWS News Blog
AWS News Blog
P
Privacy International News Feed
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
腾讯CDC
美团技术团队
宝玉的分享
宝玉的分享
博客园 - 聂微东
小众软件
小众软件
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
雷峰网
雷峰网
T
Threatpost
The Hacker News
The Hacker News
P
Palo Alto Networks Blog
L
LangChain Blog
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
T
Troy Hunt's Blog
F
Full Disclosure
博客园_首页
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
Microsoft Security Blog
Microsoft Security Blog
S
Security Affairs
量子位
Cloudbric
Cloudbric
H
Hacker News: Front Page
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LINUX DO - 最新话题
IT之家
IT之家
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Spread Privacy
Spread Privacy
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic

博客园 - 华博

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