欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 事件與委托分析

 更新時間:2009年02月16日 19:16:30   作者:  
delegate是C#中的一種類型,它實際上是一個能夠持有對某個方法的引用的類。與其它的類不同,delegate類能夠擁有一個簽名(signature),并且它只能持有與它的簽名相匹配的方法的引用。
它所實現(xiàn)的功能與C/C++中的函數(shù)指針十分相似。它允許你傳遞一個類A的方法m給另一個類B的對象,使得類B的對象能夠調(diào)用這個方法m。但與函數(shù)指針相比,delegate有許多函數(shù)指針不具備的優(yōu)點。首先,函數(shù)指針只能指向靜態(tài)函數(shù),而delegate既可以引用靜態(tài)函數(shù),又可以引用非靜態(tài)成員函數(shù)。在引用非靜態(tài)成員函數(shù)時,delegate不但保存了對此函數(shù)入口指針的引用,而且還保存了調(diào)用此函數(shù)的類實例的引用。其次,與函數(shù)指針相比,delegate是面向?qū)ο蟆㈩愋桶踩?、可靠的受控(managed)對象。也就是說,runtime能夠保證delegate指向一個有效的方法,你無須擔心delegate會指向無效地址或者越界地址。
實現(xiàn)一個delegate是很簡單的,通過以下3個步驟即可實現(xiàn)一個delegate:
1.聲明一個delegate對象,它應(yīng)當與你想要傳遞的方法具有相同的參數(shù)和返回值類型。
2. 創(chuàng)建delegate對象,并將你想要傳遞的函數(shù)作為參數(shù)傳入。
3. 在要實現(xiàn)異步調(diào)用的地方,通過上一步創(chuàng)建的對象來調(diào)用方法。using System;
public class MyDelegateTest
{
// 步驟1,聲明delegate對象
public delegate void MyDelegate(string name);
// 這是我們欲傳遞的方法,它與MyDelegate具有相同的參數(shù)和返回值類型
public static void MyDelegateFunc(string name)
{
Console.WriteLine("Hello, ", name);
}
public static void Main()
{
// 步驟2,創(chuàng)建delegate對象
MyDelegate md = new MyDelegate(MyDelegateTest.MyDelegateFunc);
// 步驟3,調(diào)用delegate
md("sam1111");
}
}
輸出結(jié)果是:Hello, sam1111
了解了delegate,下面我們來看看,在C#中對事件是如何處理的。
C#中的事件處理實際上是一種具有特殊簽名的delegate,象下面這個樣子:
public delegate void MyEventHandler(object sender, MyEventArgs e);
其中的兩個參數(shù),sender代表事件發(fā)送者,e是事件參數(shù)類。MyEventArgs類用來包含與事件相關(guān)的數(shù)據(jù),所有的事件參數(shù)類都必須從System.EventArgs類派生。當然,如果你的事件不含參數(shù),那么可以直接用System.EventArgs類作為參數(shù)。
就是這么簡單,結(jié)合delegate的實現(xiàn),我們可以將自定義事件的實現(xiàn)歸結(jié)為以下幾步:
1.定義delegate對象類型,它有兩個參數(shù),第一個參數(shù)是事件發(fā)送者對象,第二個參數(shù)是事件參數(shù)類對象。
2.定義事件參數(shù)類,此類應(yīng)當從System.EventArgs類派生。如果事件不帶參數(shù),這一步可以省略。
3.定義事件處理方法,它應(yīng)當與delegate對象具有相同的參數(shù)和返回值類型。
4. 用event關(guān)鍵字定義事件對象,它同時也是一個delegate對象。
5.用+=操作符添加事件到事件隊列中(-=操作符能夠?qū)⑹录年犃兄袆h除)。
6.在需要觸發(fā)事件的地方用調(diào)用delegate的方式寫事件觸發(fā)方法。一般來說,此方法應(yīng)為protected訪問限制,既不能以public方式調(diào)用,但可以被子類繼承。名字是OnEventName。
7. 在適當?shù)牡胤秸{(diào)用事件觸發(fā)方法觸發(fā)事件。
下面是一個簡單的例子:
復制代碼 代碼如下:

using System;
public class EventTest
{
// 步驟1,定義delegate對象
public delegate void MyEventHandler(object sender, System.EventArgs e);
// 步驟2省略
public class MyEventCls
{
// 步驟3,定義事件處理方法,它與delegate對象具有相同的參數(shù)和返回值類// 型
public void MyEventFunc(object sender, System.EventArgs e)
{
Console.WriteLine("My event is ok!");
}
}
// 步驟4,用event關(guān)鍵字定義事件對象
private event MyEventHandler myevent;
private MyEventCls myecls;
public EventTest()
{
myecls = new MyEventCls();
: // 步驟5,用+=操作符將事件添加到隊列中
this.myevent += new MyEventHandler(myecls.MyEventFunc);
}
// 步驟6,以調(diào)用delegate的方式寫事件觸發(fā)函數(shù)
protected void OnMyEvent(System.EventArgs e)
{
if(myevent != null)
myevent(this, e);
}
public void RaiseEvent()
{
EventArgs e = new EventArgs();
: // 步驟7,觸發(fā)事件
OnMyEvent(e);
}
public static void Main()
{
EventTest et = new EventTest();
Console.Write("Please input ''a'':");
string s = Console.ReadLine();
if(s == "a")
{
et.RaiseEvent();
}
else
{
Console.WriteLine("Error");
}
}
}

輸出結(jié)果如下,紅色為用戶的輸入:
Please input ‘a(chǎn)': a
My event is ok!

相關(guān)文章

最新評論