龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C#开发 >

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

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
在C#组件编程中,我们会遇到一些问题,通过本文我们能对C#组件编程中间的一些知识点有所掌握,包括Component与Control之间的区别以及Property与Attribute的区别。 1.C#组件编程的Component与

在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.  } 

精彩图集

赞助商链接