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

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - h2appy

可以使用任何字体的gvim for windows vim编辑二进制文件 数据库连接字符串大全(Database Connection Strings) 在中文下安装PrimeOCR 4.5会出现“Failed open exe”错误 让WordPress注册时不发邮件,直接显示密码 组件的初始化和终止,为什么实现 Dispose ScintillaNet 2.0的中文问题 转帖:使用 Microsoft Visual Studio 2005 时可能遇到的问题 Vmware 6.5.1正式版在Ubuntu 8.10下面运行非常缓慢的解决方法 - h2appy 给控件加上图标,让界面看起来更丰富 开源的.net图表库(A flexible charting library for .NET) 免费的中文OCR软件 转帖:一英文软件安装时出现”Failed to open EXE”问题解决 改变RichTextBox光标(caret)的形状 一款小软件gbridge 7zip美化 转帖:字符编码笔记:ASCII,Unicode和UTF-8 转帖:Endian的由来 字符编码(Character encoding)
类、组件和控件
h2appy · 2009-01-08 · via 博客园 - h2appy

转自:http://msdn.microsoft.com/zh-cn/library/0b1dk63b(VS.80).aspx

(英文)http://msdn.microsoft.com/en-us/library/0b1dk63b.aspx

类、组件和控件

本主题对组件和控件进行了定义;此处进行的讨论可以帮助您决定何时实现一个是组件或控件的类。

以下列表为实施者提供了全面的指南。

  • 如果您的类使用外部资源但不用于设计图面,则实现 System.IDisposable,或者从直接或间接实现 IDisposable 的类派生。

  • 如果您的类要用于设计图面(如 Windows 窗体或 Web 窗体设计器),则实现 System.ComponentModel.IComponent,或者从直接或间接实现 IComponent 的类派生。请注意,IComponent 扩展 IDisposable,因此 IComponent 类型始终是 IDisposable 类型。与不是 IComponentIDisposable 类型相比,IComponent 类型的性能系统开销要小。但这一不足通常可由在设计时和运行时安置 IComponent 的能力来弥补。(在本主题的后面部分将对该安置功能进行说明)。

  • 如果希望类可设计(在设计图面上使用)并且可按引用封送,可从 System.ComponentModel.Component 派生。Component 是按引用封送的 IComponent 类型的基实现。

  • 如果希望可设计的类可按值封送,可从 System.ComponentModel.MarshalByValueComponent 派生。MarshalByValueComponent 是按值封送的 IComponent 类型的基实现。

  • 如果希望向对象模型层次中引入 IComponent 类型,但由于单次继承的原因而不能从 ComponentMarshalByValueComponent 之类的基派生,请实现 IComponent

  • 如果需要提供用户界面的可设计类,则该类是控件。控件必须直接或间接从下列基控件类之一派生:System.Windows.Forms.ControlSystem.Web.UI.Control

    Note注意

    如果您的类既不是可设计类,也不保存外部资源,则不需要 IComponentIDisposable 类型。

下面是组件、控件、容器和站点的定义。

组件

在 .NET Framework 中,组件是指实现 System.ComponentModel.IComponent 接口的一个类,或从实现 IComponent 的类中直接或间接派生的类。在编程中,“组件”这个术语通常用于可重复使用并且可以和其他对象进行交互的对象。.NET Framework 组件满足这些一般要求,另外还提供诸如控制外部资源和设计时支持等功能。

控制外部资源

IComponent 接口扩展 IDisposable 接口,后者在其协定中有一个名为 Dispose 的方法。在其 Dispose 方法实现中,组件必须显式释放外部资源。与垃圾回收过程中发生的默认、不确定的清理相比,这提供了一个确定的方法来释放资源。开发人员必须在整个包容层次结构中传播 Dispose以确保组件的子级同时释放资源。另外,派生的组件必须调用其基类的 Dispose 方法。

Note注意

即使通过 Dispose 提供对资源的显式控制,也应该始终通过终结器(析构函数)提供隐式清理,以防用户未能对您的组件调用 Dispose 时资源永久泄漏。

下面的示例显示了在基组件和在派生的组件中实现 Dispose 的方式。

public class BaseComponent : IComponent {

// IComponent extends IDisposable.
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing) {
if (disposing) {
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
}

// Simply call Dispose(false).
~BaseComponent(){
Dispose (false);
}
}

// Derived component.
public class DerivedComponent : BaseComponent {

protected override void Dispose(bool disposing) {
if (disposing) {
// Free other state.
}
// You must invoke the Dispose method of the base class.
base.Dispose(disposing);
// Free your own state.
...
}
// No finalizer/destructor.
// No Dispose() method.
}

' Design pattern for a base class.
Public Class BaseComponent
Implements IComponent
' Implement IDisposable
Public Overloads Sub Dispose()
Dispose(True)
GC.SuppressFinalize(Me)
End Sub

Protected Overloads Overridable Sub Dispose(disposing As Boolean)
If disposing Then
' Free other state (managed objects).
End If
' Free your own state (unmanaged objects).
' Set large fields to null.
End Sub

Protected Overrides Sub Finalize()
' Simply call Dispose(False).
Dispose (False)
End Sub
End Class

' Design pattern for a derived component.
Public Class DerivedComponent
Inherits BaseComponent

Protected Overloads Overrides Sub Dispose(disposing As Boolean)
If disposing Then
' Release managed resources.
End If
' Release unmanaged resources.
' Set large fields to null.
' Call Dispose on your base class.
Mybase.Dispose(disposing)
End Sub
' The derived class does not have a Finalize method
' or a Dispose method with parameters because it inherits
' them from the base class.
End Class

设计时支持

在 .NET Framework 中,组件的一个重要功能就是它们是可设计的,这意味着作为组件的类可用于快速应用程序开发 (RAD) 环境(如 Visual Studio)中。可将组件添加到 Visual Studio 的工具箱中,也可将组件拖放到某个窗体上,还可以在设计图面上对组件进行操作。请注意,对 IComponent 类型的基本设计时支持已经内置于 .NET Framework 中;组件开发人员无须进行额外的工作就可利用基本设计时功能。

有关设计时支持的更多信息,请参见组件的设计时属性 (Attribute)扩展设计时支持

承载组件

组件可以被放置(承载)在一个容器(本主题稍后部分进行了定义)中。当组件被放置后,它通过其站点(本主题稍后部分进行了定义)与容器进行交互并能够通过站点从其容器查询并得到服务。为了确保容器拆卸后资源被释放,容器必须实现 IDisposable 接口。在其 Dispose 方法的实现中,容器必须释放它保留的所有资源,并调用它包含的每个组件的 Dispose 方法。

包容是逻辑上的,不需要可视的表示形式。放置数据库组件的中间层容器就是非可视包容的一个示例。可在 Visual Studio 的 Windows 窗体设计器和 Web 窗体设计器中找到可视包容。可视化设计图面是承载窗体组件(Web 窗体中的页组件)的容器。

封送组件

控件

容器和站点

如 果您正在为 Windows 窗体或 Web 窗体页(ASP.NET 页)开发组件和控件,则不需要实现容器或站点。Windows 窗体和 Web 窗体的设计器就是 Windows 窗体和 ASP.NET 服务器控件的容器。容器向放置在其中的组件和控件提供服务。在设计时,控件放置在设计器中并从设计器获得服务。为了保持完整性,以下给出了容器和站点的定 义。

Container

容器是一个实现 System.ComponentModel.IContainer 接口的类,或从实现该接口的类派生的类。容器在逻辑上包含一个或多个组件,这些组件叫做容器的子组件。

Site

站点是一个实现 System.ComponentModel.ISite 接口的类,或从实现该接口的类派生的类。站点由容器提供,用来管理其子组件及与子组件进行通信。通常,容器和站点作为一个单元来实现。

请参见

概念

属性概述
组件的设计时属性 (Attribute)

其他资源

使用 .NET Framework 开发自定义 Windows 窗体控件
开发自定义 ASP.NET 服务器控件
扩展设计时支持