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

开发 后端
在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.    
  6.  namespace Components  
  7.   {  
  8.      public class Component1 : Component  
  9.       {  
  10.          private int _id;  
  11.          private string _name;  
  12.          private DateTime _createDateTime;  
  13.    
  14.          // 在Property窗口中为灰色显示。  
  15.          public int Id  
  16.           {  
  17.               get { return _id; }  
  18.          }  
  19.    
  20.          // 在Property窗口中可以设置值。  
  21.          public string name  
  22.           {  
  23.               get { return _name; }  
  24.               set { _name = value; }  
  25.          }  
  26.    
  27.          // 在Property窗口中不可见。  
  28.          public DateTime CreateDateTime  
  29.           {  
  30.               set { _createDateTime = value; }  
  31.          }  
  32.      }  
  33.  } 

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

【编辑推荐】

  1. C#数组和指针全面讨论
  2. 简单介绍C#数组和函数
  3. C#数组初始化的应用实例解析
  4. 学习Visual C#数组速成法
  5. 浅谈C#数组工作方式

【责任编辑:彭凡 TEL:(010)68476606】

责任编辑:彭凡 来源: CSDN
相关推荐

2009-03-10 13:59:41

C#套接字编程

2009-01-16 09:58:07

C#编程C#内存管理垃圾收集

2009-08-03 13:23:04

C#编程组件-事件-委托

2009-08-27 14:12:02

C# interfac

2009-09-18 19:09:41

C# COM组件

2009-08-20 17:30:56

C#异步编程模式

2009-08-20 17:47:54

C#异步编程模式

2009-08-25 15:52:27

C#工具栏

2009-08-18 15:31:07

C# 操作Excel

2009-09-24 14:59:38

C#编写COM组件

2009-08-20 16:13:32

C#正则表达式匹配

2009-08-21 14:03:04

C#网络编程

2018-04-25 13:12:12

编程程序员建议

2009-08-31 17:02:28

C#接口编程

2015-08-27 11:16:14

ios开发技巧

2011-03-31 14:16:54

Cacti技巧

2009-04-29 09:06:18

C#设计模式Adapter

2009-09-17 16:55:58

C#组件设计

2013-04-09 11:26:55

WindowsPhon

2009-07-31 18:39:31

C#中foreach引
点赞
收藏

51CTO技术栈公众号