在C#使用字典存儲事件示例及實現(xiàn)自定義事件訪問器
使用字典存儲事件實例
accessor-declarations 的一種用法是公開很多事件但不為每個事件分配字段,而是使用字典來存儲這些事件實例。這只在具有很多事件但您預計大多數(shù)事件都不會實現(xiàn)時才有用。
public delegate void EventHandler1(int i); public delegate void EventHandler2(string s); public class PropertyEventsSample { private System.Collections.Generic.Dictionary<string, System.Delegate> eventTable; public PropertyEventsSample() { eventTable = new System.Collections.Generic.Dictionary<string, System.Delegate>(); eventTable.Add("Event1", null); eventTable.Add("Event2", null); } public event EventHandler1 Event1 { add { lock (eventTable) { eventTable["Event1"] = (EventHandler1)eventTable["Event1"] + value; } } remove { lock (eventTable) { eventTable["Event1"] = (EventHandler1)eventTable["Event1"] - value; } } } public event EventHandler2 Event2 { add { lock (eventTable) { eventTable["Event2"] = (EventHandler2)eventTable["Event2"] + value; } } remove { lock (eventTable) { eventTable["Event2"] = (EventHandler2)eventTable["Event2"] - value; } } } internal void RaiseEvent1(int i) { EventHandler1 handler1; if (null != (handler1 = (EventHandler1)eventTable["Event1"])) { handler1(i); } } internal void RaiseEvent2(string s) { EventHandler2 handler2; if (null != (handler2 = (EventHandler2)eventTable["Event2"])) { handler2(s); } } } public class TestClass { public static void Delegate1Method(int i) { System.Console.WriteLine(i); } public static void Delegate2Method(string s) { System.Console.WriteLine(s); } static void Main() { PropertyEventsSample p = new PropertyEventsSample(); p.Event1 += new EventHandler1(TestClass.Delegate1Method); p.Event1 += new EventHandler1(TestClass.Delegate1Method); p.Event1 -= new EventHandler1(TestClass.Delegate1Method); p.RaiseEvent1(2); p.Event2 += new EventHandler2(TestClass.Delegate2Method); p.Event2 += new EventHandler2(TestClass.Delegate2Method); p.Event2 -= new EventHandler2(TestClass.Delegate2Method); p.RaiseEvent2("TestString"); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } }
輸出:
2 TestString
實現(xiàn)自定義事件訪問器
事件是特殊類型的多路廣播委托,只能從聲明它的類中調用。客戶端代碼通過提供對應在引發(fā)事件時調用的方法的引用來訂閱事件。這些方法通過事件訪問器添加到委托的調用列表中,事件訪問器類似于屬性訪問器,不同之處在于事件訪問器被命名為 add 和 remove。在大多數(shù)情況下都不需要提供自定義的事件訪問器。如果您在代碼中沒有提供自定義的事件訪問器,編譯器會自動添加事件訪問器。但在某些情況下,您可能需要提供自定義行為。下面的示例演示如何實現(xiàn)自定義的 add 和 remove 事件訪問器。雖然可以替換這些訪問器內的任何代碼,但建議您在添加或移除新的事件處理程序方法之前先鎖定該事件。
event EventHandler IDrawingObject.OnDraw { add { lock (PreDrawEvent) { PreDrawEvent += value; } } remove { lock (PreDrawEvent) { PreDrawEvent -= value; } } }
上一篇:C#程序中創(chuàng)建、復制、移動、刪除文件或文件夾的示例
欄 目:C#教程
下一篇:C#提高編程能力的50個要點總結
本文標題:在C#使用字典存儲事件示例及實現(xiàn)自定義事件訪問器
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6709.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#實現(xiàn)將窗體固定在顯示器的左上角且不能移動的方法
- 01-10C#使用windows服務開啟應用程序的方法
- 01-10C#實現(xiàn)在Form里面內嵌dos窗體的方法
- 01-10c# ArrayList的使用方法小總結
- 01-10C#中查找Dictionary中的重復值的方法
- 01-10C#實現(xiàn)在啟動目錄創(chuàng)建快捷方式的方法
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子