c# 实现接口事件
实现接口事件的规则与实现任何接口方法或属性的规则基本相同。
namespace ImplementInterfaceEvents { public interface IDrawingObject { event EventHandler ShapeChanged; } public class MyEventArgs : EventArgs { // class members } public class Shape : IDrawingObject { public event EventHandler ShapeChanged; void ChangeShape() { // Do something here before the event… OnShapeChanged(new MyEventArgs(/*arguments*/)); // or do something here after the event. } protected virtual void OnShapeChanged(MyEventArgs e) { if(ShapeChanged != null) { ShapeChanged(this, e); } } } }
这两个事件访问器通常由编译器提供,但在这种情况下编译器不能提供。
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(this, new 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(this, new 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 137 } 138 /* Output: 139 Sub1 receives the IDrawingObject event. 140 Drawing a shape. 141 Sub2 receives the IShape event. 142 */
摘自http://msdn.microsoft.com/zh-cn/library/ak9w5846.aspx
- 上一篇 »C#中接口与类的区别
- 下一篇 »C#中的接口