C#接口事件的实现解析

开发 后端
C#接口事件的实现是如何办到的呢?C#接口事件的实现具体的步骤和需要注意的事项,那么本文就向你介绍具体的内容。

C#接口事件的实现是如何的呢?下面的C#接口事件示例演示如何在类中实现接口事件。实现C#接口事件的规则与实现任何接口方法或属性的规则基本相同。

C#接口事件实例:

在类中实现接口事件,在类中声明事件,然后在适当的区域调用该事件。

  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#接口事件示例

下面的示例演示如何处理以下的不常见情况:您的类是从两个以上的接口继承的,每个接口都含有同名事件)。在这种情况下,您至少要为其中一个事件提供显式接口实现。为事件编写显式接口实现时,必须编写 add 和 remove 事件访问器。这两个事件访问器通常由编译器提供,但在这种情况下编译器不能提供。

您可以提供自己的访问器,以便指定这两个事件是由您的类中的同一事件表示,还是由不同事件表示。例如,根据接口规范,如果事件应在不同时间引发,则可以将每个事件与类中的一个单独实现关联。在下面的示例中,订户将形状引用强制转换为 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#接口事件的实现以及使用的一些内容就向你介绍到这里,希望对你了解和学习C#接口事件有所帮助。

【编辑推荐】

  1. C#接口的定义详解
  2. C#接口编程之接口成员浅析
  3. C#实现接口的实例解析
  4. C#接口的作用实例解析
  5. C#接口实例应用的的深入探讨
责任编辑:仲衡 来源: CSDN
相关推荐

2009-08-31 17:16:12

C#实现接口

2009-08-31 17:30:10

C#接口的作用

2009-08-31 17:47:43

C#接口使用

2009-08-27 17:40:21

C#接口的作用

2009-08-31 18:17:32

C#接口编程

2009-08-25 17:55:52

C#实现Strateg

2009-08-31 15:55:17

C#实现Strateg

2011-08-23 17:11:13

Lua事件C#

2009-08-24 10:06:31

C#接口成员

2009-08-31 16:48:02

C#实现IDispos

2009-08-24 10:47:45

C#接口重实现

2009-08-31 16:23:13

C#接口

2009-09-07 15:27:04

C# MessageB

2009-09-01 18:29:24

C#实现多个接口

2009-09-04 13:22:31

C#实现多个接口

2009-09-02 16:30:20

C#定义数组

2009-09-03 16:38:49

C#回车键事件

2009-09-09 11:29:32

C# TextBox事

2009-08-27 18:09:49

C#接口的实现

2009-08-31 18:34:57

C#接口事件
点赞
收藏

51CTO技术栈公众号