自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析C#組件編程中的一些小細(xì)節(jié)

開發(fā) 后端
在C#組件編程中,我們會(huì)談到一些Component與Control之間的區(qū)別等等,希望對(duì)大家有所幫助。

在C#組件編程中,我們會(huì)遇到一些問題,通過本文我們能對(duì)C#組件編程中間的一些知識(shí)點(diǎn)有所掌握,包括Component與Control之間的區(qū)別以及Property與Attribute的區(qū)別。

1.C#組件編程的Component與Control之間的區(qū)別

 (1)Component在Run Time時(shí)不能呈現(xiàn)UI,而Control可以在Run Time時(shí)呈現(xiàn)UI(但是Visual Studio 2005里的asp.net中的SqlDataSource是Control,但是它不能呈現(xiàn)UI)。

 (2)Component是貼在容器Container上的,而Control則是貼在Windows Form或者Web Form上的。舉例來說,SqlCommand是個(gè)Component,DataGrid則是一個(gè)Control。

2.Property與Attribute的區(qū)別

在中文中這兩個(gè)是沒有區(qū)別的,本文將從字面上給以區(qū)別:Property表示屬性,Attribute表示特性.

3.一個(gè)簡(jiǎn)單的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窗口中可以設(shè)置值。  
  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所屬的哪個(gè)組。

DescriptionAttribute:Property或者Event的簡(jiǎn)單描述。

ReadOnlyAttribute : Property在屬性窗口中不可改寫

DefaultEventAttribute:默認(rèn)Event、選中組件,其Event窗口中默認(rèn)選中在這個(gè)Event上。

DefaultPropertyAttribute:默認(rèn)Property,選中組件,其Property窗口中默認(rèn)選中在這個(gè)Property上。

DefaultValueAttribute:Property的默認(rèn)值

EditorAttribute:指定Property Editor使用的編輯器。 

DesignerSerializationVisibilityAttribute:指定通過Property Editor得到的結(jié)果是否保存在代碼中。 

LocalizableAttribute:用戶要本地化某個(gè)窗體時(shí),任何具有該特性的屬性都將自動(dòng)***駐留到資源文件中。

--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")]  // 可以在一個(gè)[]里寫兩個(gè)Attribute。  
  28.          public string Sex  
  29.           {  
  30.               get { return _sex; }  
  31.               set { _sex = value; }  
  32.          }  
  33.    
  34.          //屬性默認(rèn)值為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#數(shù)組和指針全面討論
  2. 簡(jiǎn)單介紹C#數(shù)組和函數(shù)
  3. C#數(shù)組初始化的應(yīng)用實(shí)例解析
  4. 學(xué)習(xí)Visual C#數(shù)組速成法
  5. 淺談C#數(shù)組工作方式

【責(zé)任編輯:彭凡 TEL:(010)68476606】

責(zé)任編輯:彭凡 來源: CSDN
相關(guān)推薦

2009-01-16 09:58:07

C#編程C#內(nèi)存管理垃圾收集

2009-03-10 13:59:41

C#套接字編程

2009-08-03 13:23:04

C#編程組件-事件-委托

2009-08-27 14:12:02

C# interfac

2009-08-20 17:30:56

C#異步編程模式

2009-09-18 19:09:41

C# COM組件

2009-08-25 15:52:27

C#工具欄

2009-08-20 17:47:54

C#異步編程模式

2009-08-20 16:13:32

C#正則表達(dá)式匹配

2009-08-31 17:02:28

C#接口編程

2009-08-21 14:03:04

C#網(wǎng)絡(luò)編程

2009-09-24 14:59:38

C#編寫COM組件

2015-08-27 11:16:14

ios開發(fā)技巧

2011-03-31 14:16:54

Cacti技巧

2018-04-25 13:12:12

編程程序員建議

2009-08-18 15:31:07

C# 操作Excel

2009-04-29 09:06:18

C#設(shè)計(jì)模式Adapter

2013-04-09 11:26:55

WindowsPhon

2009-07-31 18:39:31

C#中foreach引

2009-08-21 14:47:39

C#網(wǎng)絡(luò)編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)