浅析C#组件编程中的一些小细节

时间:2023-03-08 16:57:28
控件与组件的区别(Control&Component的区别)
作者:作者不详  发布日期:2011-06-30 12:08:41
控件与组件的区别(Control&Component的区别)

一般把Control翻译成控件,把Component翻译成组件。

控件就是具有用户界面的组件。要说的具体一点,就得回顾早期 Windows 的历史根源,当时控件指任何子窗口——按钮、列表框、编辑框或者某个对话框中的静态文本。从概念上讲,这些窗口——控件——类似用来操作收音机或小电器的旋钮和按钮。随着控件数量的增加(组合框、日期时间控件等等),控件逐渐成为子窗口的代名词,无论是用在对话框中还是用在其它种类的主窗口中。没过多久 BASIC 程序员开始编写他们自己专用的控件,自然而然地人们便想到共享这些控件。共享代码的方法之一是通过磁盘拷贝,但那样显然效率低下。必须要有一种机制使开发者建立的控件能够在其它程序员的应用中轻而易举地插入,这便是VBA控件,OLE控件,OCX和最后ActiveX 控件的动机。

这就是控件和组件之间产生混淆之所在。因为为了解决控件的可复用问题,所有这些技术必须首先解决更为一般的组件重用问题。(COM,如果你还记得它的话,意思是组件对象模型)。在软件行话中,组件这个术语指任何可复用的对象或任何可与其它对象交互的代码体。子程序的发明,曾经一度成为程序员趋之若鹜的软件工程圣杯:一种统一的编程理论,它使程序员从基本构建块——也就是用所选语言编写的各种组件建立大型系统。从子程序演变到OOP,到DLLs,再到COM,再到.NET框架的每一种新的编程范例都代表了一种不同的提供可重用性的方案。VBX使用DLLs的固化名称。COM使用接口和IUnknown。.NET框架使用微软的中间语言(MSIL)层和公共语言运行时(CLR)来提供统一的粘合。

因此,控件是组件的一个主要样本(并且历史上曾驱动着组件的开发),控件又不仅仅是唯一的一种组件。组件不需要显示任何信息或用户界面。组件可能实现科学计算,收集性能数据,计算1971年1月1日到现在的毫秒数,仰或是读取布什总统竞选活动保险箱里的美金数。

Figure 4 显示了 Visual Studio .NET 中的非控件组件例子。 
浅析C#组件编程中的一些小细节

在 .NET 框架中,术语控件和组件为 .NET 赋予了专门的意义。Component 类为被用于设计层面的对象如 Windows Forms Designer (Windows 窗体设计器)或 Web Forms Designer (Web 窗体设计器)提供了基本实现。某个 Component 是任何可以被拽到某个窗体的任何东西。Component 类实现IComponent,ISite 和 IContainer。这些接口比起其来自 OLE 时期的 COM 堂兄弟要简单得多。 IContainer 比起带有 Add/Remove 方法的组件列表以及组件属性来要稍微复杂一点,它获得的组件是一个 ComponentCollection (组件集合)。

IComponent 从 IDisposable 派生而来,并且只有一个属性,Site,获取组件的ISite接口。Component 可能有,也可能没有Site。ISite 有四个属性,其中包括Name和DesignMode,它控制该组件是否处于——还能是什么?——设计模式。ISite 派生于另一个接口,IServiceProvider,它只有一个方法,GetService。在COM中,IServiceProvider 类似 QueryInterface——用它可以通过ID来查询某个对象的接口,但是与 QueryInterface 不同的是该对象本身不用去实现这个接口,它仅仅知道在哪里和如何获取它即可。同样,在.NET框架中,IServiceProvider 是一种获取其它接口或对象的通用方法——服务——对象不用实现它就知道的一种服务。

.NET框架使得编写可复用组件轻松自如,不再需要 IDL,不再需要类型定义语言,不再需要费力的设计时支持。通过反射(reflection)的魔法,CLR 从代码本身就已经知道了该知道的一切,所有的类都在掌控之中。为了添加设计时支持,你只要用额外的设计器标记你的属性即可。例如,在托管C++中:

C# Code:

// in CMyControl 
[Category(S"Appearance")]
[Description(S"Specifies widget foreground color.")]
_property Color get_ForeColor() { ... }
_property void set_ForeColor(Color value) { ... }

现在窗体设计器在“外观”(Appearance)中列出你的 ForeColor 属性并使用帮助描述(Description)。有关设计时属性的更多内容,请参考.NET框架文档中的“组件的设计时属性”

浅析C#组件编程中的一些小细节

Figure 5 显示了.NET框架中的类层次结构,它能说明上述讨论的问题。正如你所看到的,Control 从Component 派生而来。这是用另外一种方式来说明控件即组件(反之则不然)。更具体地讲,控件是一个用用户界面的组件——能绘制东西并能与用户交互。Control 类还是所有托管窗口类的基类——窗体、按钮、栅格、面板、工具栏等等。Control 类是定义 WndProc 和 ClientSize 以及所有标准窗口事件如 GotFocus 和 Click 的地方。Web控件(System.Web.UI.Control)也是组件,不过从严格的意义上讲,它不是从 System.ComponentModel.Component 派生的。(对于 Web 控件,其名字空间为 System.Web.UI,Control 本身实现 IComponent。)

除了实现 IComponent 之外,System.ComponentModel.Component 还提供了所有组件需要的列集支持,但它是通过从 MarshalByRefObject 派生来实现的。如果想生成一个值列集组件,可以从 MarshalByValueComponent 派生(它实现了 IComponent,IDisposable 和 IServiceProvider)。System.Data.DataColumn,DataSet 和 DataTable 都是是值列集组件的例子。这些对象跨机器/进程边界传递其实际数据。

如果你正在编写其他人也能用窗体设计器拖拽到其窗体的可重用的小组件,那么你必须从 Component 派生。如果你的小组件还具备用户界面——能创建窗口,绘画或与用户交互——那么就应该从 Control 派生。

转:

http://www.vckbase.com/document/viewdoc/?id=1294#%E6%8E%A7%E4%BB%B6%E5%92%8C%E7%BB%84%E4%BB%B6

浅析C#组件编程中的一些小细节

2009-11-03  来自:网上整理  字体大小:【     
  • 摘要:在C#组件编程中,我们会谈到一些Component与Control之间的区别等等,希望对大家有所帮助

在C#组件编程中,我们会遇到一些问题,通过本文我们能对C#组件编程中间的一些知识点有所掌握,包括Component与Control之间的区别以及Property与Attribute的区别。

1.C#组件编程的Component与Control之间的区别

(1)Component在Run Time时不能呈现UI,而Control可以在Run Time时呈现UI(但是Visual Studio 2005里的asp.net中的SqlDataSource是Control,但是它不能呈现UI)。

(2)Component是贴在容器Container上的,而Control则是贴在Windows Form或者Web Form上的。举例来说,SqlCommand是个Component,DataGrid则是一个Control。

2.Property与Attribute的区别

在中文中这两个是没有区别的,本文将从字面上给以区别:Property表示属性,Attribute表示特性.

3.一个简单的Component

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ComponentModel;
  5. namespace Components
  6. {
  7. public class Component1 : Component
  8. {
  9. private int _id;
  10. private string _name;
  11. private DateTime _createDateTime;
  12. // 在Property窗口中为灰色显示。
  13. public int Id
  14. {
  15. get { return _id; }
  16. }
  17. // 在Property窗口中可以设置值。
  18. public string name
  19. {
  20. get { return _name; }
  21. set { _name = value; }
  22. }
  23. // 在Property窗口中不可见。
  24. public DateTime CreateDateTime
  25. {
  26. set { _createDateTime = value; }
  27. }
  28. }
  29. }

4.各种Attribute

--1.Attribute列表

EventAttribute有:

BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultEventAttribute

PropertyAttribute有:

BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultPropertyAttribute、ReadOnlyAttribute、 DefaultValueAttribute、EditorAttribute 
、DesignerSerializationVisibilityAttribute、TypeConverterAttribute、BindableAttribute、LocalizableAttribute

MethodAttribute有:

WebMethod

--2.Attribute用途:

BrowsableAttribute:在Property窗口中是否可见。

CategoryAttribute:Property或者Event所属的哪个组。

DescriptionAttribute:Property或者Event的简单描述。

ReadOnlyAttribute : Property在属性窗口中不可改写

DefaultEventAttribute:默认Event、选中组件,其Event窗口中默认选中在这个Event上。

DefaultPropertyAttribute:默认Property,选中组件,其Property窗口中默认选中在这个Property上。

DefaultValueAttribute:Property的默认值

EditorAttribute:指定Property Editor使用的编辑器。

DesignerSerializationVisibilityAttribute:指定通过Property Editor得到的结果是否保存在代码中。

LocalizableAttribute:用户要本地化某个窗体时,任何具有该特性的属性都将自动永久驻留到资源文件中。

--3.使用示例:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using System.ComponentModel;
    5. namespace Components
    6. {
    7. // PropertyAttribute、EventAttribute分别放在Property、Event上,并用[]括起来。
    8. // DefaultPropertyAttribute、DefaultEventAttribute必须放在类头上。
    9. [DefaultEvent("CustomerLogout")]
    10. public class Customer : Component
    11. {
    12. private string _id;
    13. private string _sex;
    14. private int _age=20;
    15. private string _address;
    16. private DateTime _createTime;
    17. // 没有CategoryAttribute、DescriptionAttribute。
    18. public string Id
    19. {
    20. get { return _id; }
    21. set { _id = value; }
    22. }
    23. // 此属性在Customer's Details分组中,CategoryAttribute、DescriptionAttribute也适用于Event。
    24. [Category("Customer's Details"), Description("Customer's Sex")]  // 可以在一个[]里写两个Attribute。
    25. public string Sex
    26. {
    27. get { return _sex; }
    28. set { _sex = value; }
    29. }
    30. //属性默认值为20
    31. [Category("Customer's Details")]
    32. [Description("Customer's Age"), DefaultValue(20)]
    33. public int Age
    34. {
    35. get { return _age; }
    36. set { _age = value; }
    37. }
    38. [DefaultValue("shanghai"),Category("Customer's Details")]
    39. public string Address
    40. {
    41. get { return _address; }
    42. set { _address = value; }
    43. }
    44. // 此Property在Property窗口中不可见,BrowsableAttribute也适用于Event。
    45. [Browsable(false)]
    46. public DateTime CreateTime
    47. {
    48. get { return _createTime; }
    49. set { _createTime = value; }
    50. }
    51. public sealed class CustomerLoginEventArgs : EventArgs
    52. { }
    53. public sealed class CustomerLogoutEventArgs : EventArgs
    54. { }
    55. public delegate void CustomerLoginEventHandler(object sender, CustomerLoginEventArgs e);
    56. public delegate void CustomerLogoutEventHandler(object sender, CustomerLogoutEventArgs e);
    57. public event CustomerLoginEventHandler CustomerLogin
    58. {
    59. add { }
    60. remove { }
    61. }
    62. public event CustomerLogoutEventHandler CustomerLogout
    63. {
    64. add { }
    65. remove { }
    66. }
    67. }
    68. }