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

C#接口事件的實現(xiàn)解析

開發(fā) 后端
C#接口事件的實現(xiàn)是如何辦到的呢?C#接口事件的實現(xiàn)具體的步驟和需要注意的事項,那么本文就向你介紹具體的內(nèi)容。

C#接口事件的實現(xiàn)是如何的呢?下面的C#接口事件示例演示如何在類中實現(xiàn)接口事件。實現(xiàn)C#接口事件的規(guī)則與實現(xiàn)任何接口方法或?qū)傩缘囊?guī)則基本相同。

C#接口事件實例:

在類中實現(xiàn)接口事件,在類中聲明事件,然后在適當?shù)膮^(qū)域調(diào)用該事件。

  1. public interface IDrawingObject  
  2. {  
  3. event EventHandler ShapeChanged;  
  4. }  
  5. public class MyEventArgs : EventArgs {…}  
  6. public class Shape : IDrawingObject  
  7. {  
  8. event EventHandler ShapeChanged;  
  9. void ChangeShape()  
  10. {  
  11. // Do something before the event…  
  12. OnShapeChanged(new MyEventsArgs(…));  
  13. // or do something after the event.   
  14. }  
  15. protected virtual void OnShapeChanged(MyEventArgs e)  
  16. {  
  17. if(ShapeChanged != null)  
  18. {  
  19.    ShapeChanged(this, e);  
  20. }  
  21. }  

C#接口事件示例

下面的示例演示如何處理以下的不常見情況:您的類是從兩個以上的接口繼承的,每個接口都含有同名事件)。在這種情況下,您至少要為其中一個事件提供顯式接口實現(xiàn)。為事件編寫顯式接口實現(xiàn)時,必須編寫 add 和 remove 事件訪問器。這兩個事件訪問器通常由編譯器提供,但在這種情況下編譯器不能提供。

您可以提供自己的訪問器,以便指定這兩個事件是由您的類中的同一事件表示,還是由不同事件表示。例如,根據(jù)接口規(guī)范,如果事件應在不同時間引發(fā),則可以將每個事件與類中的一個單獨實現(xiàn)關(guān)聯(lián)。在下面的示例中,訂戶將形狀引用強制轉(zhuǎn)換為 IShape 或 IDrawingObject,從而確定自己將會接收哪個 OnDraw 事件。

C#接口事件代碼:

  1. namespace WrapTwoInterfaceEvents  
  2. {  
  3. using System;  
  4.  
  5. public interface IDrawingObject  
  6. {  
  7. // Raise this event before drawing  
  8. // the object.  
  9. event EventHandler OnDraw;  
  10. }  
  11. public interface IShape  
  12. {  
  13. // Raise this event after drawing  
  14. // the shape.  
  15. event EventHandler OnDraw;  
  16. }  
  17.  
  18.  
  19. // Base class event publisher inherits two  
  20. // interfaces, each with an OnDraw event  
  21. public class Shape : IDrawingObject, IShape  
  22. {  
  23. // Create an event for each interface event  
  24. event EventHandler PreDrawEvent;  
  25. event EventHandler PostDrawEvent;  
  26.  
  27. object objectLock = new Object();  
  28.  
  29. // Explicit interface implementation required.  
  30. // Associate IDrawingObject's event with  
  31. // PreDrawEvent  
  32. event EventHandler IDrawingObject.OnDraw  
  33. {  
  34. add  
  35. {  
  36. lock (objectLock)  
  37. {  
  38. PreDrawEvent += value;  
  39. }  
  40. }  
  41. remove  
  42. {  
  43. lock (objectLock)  
  44. {  
  45. PreDrawEvent -= value;  
  46. }  
  47. }  
  48. }  
  49. // Explicit interface implementation required.  
  50. // Associate IShape's event with  
  51. // PostDrawEvent  
  52. event EventHandler IShape.OnDraw  
  53. {  
  54. add   
  55. {  
  56. lock (objectLock)  
  57. {  
  58. PostDrawEvent += value;  
  59. }  
  60. }  
  61. remove  
  62. {  
  63. lock (objectLock)  
  64. {  
  65. PostDrawEvent -= value;  
  66. }  
  67. }  
  68.  
  69.  
  70. }  
  71.  
  72. // For the sake of simplicity this one method  
  73. // implements both interfaces.   
  74. public void Draw()  
  75. {  
  76. // Raise IDrawingObject's event before the object is drawn.  
  77. EventHandler handler = PreDrawEvent;  
  78. if (handler != null)  
  79. {  
  80. handler(thisnew EventArgs());  
  81. }  
  82. Console.WriteLine("Drawing a shape.");  
  83.  
  84. // RaiseIShape's event after the object is drawn.  
  85. handler = PostDrawEvent;  
  86. if (handler != null)  
  87. {  
  88. handler(thisnew EventArgs());  
  89. }  
  90. }  
  91. }  
  92. public class Subscriber1  
  93. {  
  94. // References the shape object as an IDrawingObject  
  95. public Subscriber1(Shape shape)  
  96. {  
  97. IDrawingObject d = (IDrawingObject)shape;  
  98. d.OnDraw += new EventHandler(d_OnDraw);  
  99. }  
  100.  
  101. void d_OnDraw(object sender, EventArgs e)  
  102. {  
  103. Console.WriteLine("Sub1 receives the IDrawingObject event.");  
  104. }  
  105. }  
  106. // References the shape object as an IShape  
  107. public class Subscriber2  
  108. {  
  109. public Subscriber2(Shape shape)  
  110. {  
  111. IShape d = (IShape)shape;  
  112. d.OnDraw += new EventHandler(d_OnDraw);  
  113. }  
  114.  
  115. void d_OnDraw(object sender, EventArgs e)  
  116. {  
  117. Console.WriteLine("Sub2 receives the IShape event.");  
  118. }  
  119. }  
  120.  
  121.  
  122. public class Program  
  123. {  
  124. static void Main(string[] args)  
  125. {  
  126. Shape shape = new Shape();  
  127. Subscriber1 sub = new Subscriber1(shape);  
  128. Subscriber2 sub2 = new Subscriber2(shape);  
  129. shape.Draw();  
  130.  
  131. // Keep the console window open in debug mode.  
  132. System.Console.WriteLine("Press any key to exit.");  
  133. System.Console.ReadKey();  
  134. }  
  135. }  
  136.  
  1. /* C#接口事件示例Output:  
  2. Sub1 receives the IDrawingObject event.  
  3. Drawing a shape.  
  4. Sub2 receives the IShape event.  
  5. */ 

C#接口事件的實現(xiàn)以及使用的一些內(nèi)容就向你介紹到這里,希望對你了解和學習C#接口事件有所幫助。

【編輯推薦】

  1. C#接口的定義詳解
  2. C#接口編程之接口成員淺析
  3. C#實現(xiàn)接口的實例解析
  4. C#接口的作用實例解析
  5. C#接口實例應用的的深入探討
責任編輯:仲衡 來源: CSDN
相關(guān)推薦

2009-08-31 17:16:12

C#實現(xiàn)接口

2009-08-31 17:47:43

C#接口使用

2009-08-27 17:40:21

C#接口的作用

2009-08-31 17:30:10

C#接口的作用

2009-08-31 18:17:32

C#接口編程

2009-08-31 15:55:17

C#實現(xiàn)Strateg

2009-08-25 17:55:52

C#實現(xiàn)Strateg

2011-08-23 17:11:13

Lua事件C#

2009-08-24 10:06:31

C#接口成員

2009-09-02 16:30:20

C#定義數(shù)組

2009-09-07 15:27:04

C# MessageB

2009-09-03 16:38:49

C#回車鍵事件

2009-09-04 13:22:31

C#實現(xiàn)多個接口

2009-09-01 18:29:24

C#實現(xiàn)多個接口

2009-08-31 16:48:02

C#實現(xiàn)IDispos

2009-08-24 10:47:45

C#接口重實現(xiàn)

2009-08-31 16:23:13

C#接口

2009-09-09 11:29:32

C# TextBox事

2009-09-09 13:18:10

C# TextBox滾C# TextBox

2009-08-27 18:09:49

C#接口的實現(xiàn)
點贊
收藏

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