淺析C#組件編程中的一些小細(xì)節(jié)
在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
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ComponentModel;
- namespace Components
- {
- public class Component1 : Component
- {
- private int _id;
- private string _name;
- private DateTime _createDateTime;
- // 在Property窗口中為灰色顯示。
- public int Id
- {
- get { return _id; }
- }
- // 在Property窗口中可以設(shè)置值。
- public string name
- {
- get { return _name; }
- set { _name = value; }
- }
- // 在Property窗口中不可見。
- public DateTime CreateDateTime
- {
- set { _createDateTime = value; }
- }
- }
- }
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.使用示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ComponentModel;
- namespace Components
- {
- // PropertyAttribute、EventAttribute分別放在Property、Event上,并用[]括起來。
- // DefaultPropertyAttribute、DefaultEventAttribute必須放在類頭上。
- [DefaultEvent("CustomerLogout")]
- public class Customer : Component
- {
- private string _id;
- private string _sex;
- private int _age=20;
- private string _address;
- private DateTime _createTime;
- // 沒有CategoryAttribute、DescriptionAttribute。
- public string Id
- {
- get { return _id; }
- set { _id = value; }
- }
- // 此屬性在Customer's Details分組中,CategoryAttribute、DescriptionAttribute也適用于Event。
- [Category("Customer's Details"), Description("Customer's Sex")] // 可以在一個(gè)[]里寫兩個(gè)Attribute。
- public string Sex
- {
- get { return _sex; }
- set { _sex = value; }
- }
- //屬性默認(rèn)值為20
- [Category("Customer's Details")]
- [Description("Customer's Age"), DefaultValue(20)]
- public int Age
- {
- get { return _age; }
- set { _age = value; }
- }
- [DefaultValue("shanghai"),Category("Customer's Details")]
- public string Address
- {
- get { return _address; }
- set { _address = value; }
- }
- // 此Property在Property窗口中不可見,BrowsableAttribute也適用于Event。
- [Browsable(false)]
- public DateTime CreateTime
- {
- get { return _createTime; }
- set { _createTime = value; }
- }
- public sealed class CustomerLoginEventArgs : EventArgs
- { }
- public sealed class CustomerLogoutEventArgs : EventArgs
- { }
- public delegate void CustomerLoginEventHandler(object sender, CustomerLoginEventArgs e);
- public delegate void CustomerLogoutEventHandler(object sender, CustomerLogoutEventArgs e);
- public event CustomerLoginEventHandler CustomerLogin
- {
- add { }
- remove { }
- }
- public event CustomerLogoutEventHandler CustomerLogout
- {
- add { }
- remove { }
- }
- }
- }
【編輯推薦】
- C#數(shù)組和指針全面討論
- 簡(jiǎn)單介紹C#數(shù)組和函數(shù)
- C#數(shù)組初始化的應(yīng)用實(shí)例解析
- 學(xué)習(xí)Visual C#數(shù)組速成法
- 淺談C#數(shù)組工作方式
【責(zé)任編輯:彭凡 TEL:(010)68476606】