C#接口事件的實現(xiàn)解析
C#接口事件的實現(xiàn)是如何的呢?下面的C#接口事件示例演示如何在類中實現(xiàn)接口事件。實現(xiàn)C#接口事件的規(guī)則與實現(xiàn)任何接口方法或?qū)傩缘囊?guī)則基本相同。
C#接口事件實例:
在類中實現(xiàn)接口事件,在類中聲明事件,然后在適當?shù)膮^(qū)域調(diào)用該事件。
- public interface IDrawingObject
- {
- event EventHandler ShapeChanged;
- }
- public class MyEventArgs : EventArgs {…}
- public class Shape : IDrawingObject
- {
- event EventHandler ShapeChanged;
- void ChangeShape()
- {
- // Do something before the event…
- OnShapeChanged(new MyEventsArgs(…));
- // or do something after the event.
- }
- protected virtual void OnShapeChanged(MyEventArgs e)
- {
- if(ShapeChanged != null)
- {
- ShapeChanged(this, e);
- }
- }
- }
C#接口事件示例
下面的示例演示如何處理以下的不常見情況:您的類是從兩個以上的接口繼承的,每個接口都含有同名事件)。在這種情況下,您至少要為其中一個事件提供顯式接口實現(xiàn)。為事件編寫顯式接口實現(xiàn)時,必須編寫 add 和 remove 事件訪問器。這兩個事件訪問器通常由編譯器提供,但在這種情況下編譯器不能提供。
您可以提供自己的訪問器,以便指定這兩個事件是由您的類中的同一事件表示,還是由不同事件表示。例如,根據(jù)接口規(guī)范,如果事件應在不同時間引發(fā),則可以將每個事件與類中的一個單獨實現(xiàn)關(guān)聯(lián)。在下面的示例中,訂戶將形狀引用強制轉(zhuǎn)換為 IShape 或 IDrawingObject,從而確定自己將會接收哪個 OnDraw 事件。
C#接口事件代碼:
- namespace WrapTwoInterfaceEvents
- {
- using System;
- public interface IDrawingObject
- {
- // Raise this event before drawing
- // the object.
- event EventHandler OnDraw;
- }
- public interface IShape
- {
- // Raise this event after drawing
- // the shape.
- event EventHandler OnDraw;
- }
- // Base class event publisher inherits two
- // interfaces, each with an OnDraw event
- public class Shape : IDrawingObject, IShape
- {
- // Create an event for each interface event
- event EventHandler PreDrawEvent;
- event EventHandler PostDrawEvent;
- object objectLock = new Object();
- // Explicit interface implementation required.
- // Associate IDrawingObject's event with
- // PreDrawEvent
- event EventHandler IDrawingObject.OnDraw
- {
- add
- {
- lock (objectLock)
- {
- PreDrawEvent += value;
- }
- }
- remove
- {
- lock (objectLock)
- {
- PreDrawEvent -= value;
- }
- }
- }
- // Explicit interface implementation required.
- // Associate IShape's event with
- // PostDrawEvent
- event EventHandler IShape.OnDraw
- {
- add
- {
- lock (objectLock)
- {
- PostDrawEvent += value;
- }
- }
- remove
- {
- lock (objectLock)
- {
- PostDrawEvent -= value;
- }
- }
- }
- // For the sake of simplicity this one method
- // implements both interfaces.
- public void Draw()
- {
- // Raise IDrawingObject's event before the object is drawn.
- EventHandler handler = PreDrawEvent;
- if (handler != null)
- {
- handler(this, new EventArgs());
- }
- Console.WriteLine("Drawing a shape.");
- // RaiseIShape's event after the object is drawn.
- handler = PostDrawEvent;
- if (handler != null)
- {
- handler(this, new EventArgs());
- }
- }
- }
- public class Subscriber1
- {
- // References the shape object as an IDrawingObject
- public Subscriber1(Shape shape)
- {
- IDrawingObject d = (IDrawingObject)shape;
- d.OnDraw += new EventHandler(d_OnDraw);
- }
- void d_OnDraw(object sender, EventArgs e)
- {
- Console.WriteLine("Sub1 receives the IDrawingObject event.");
- }
- }
- // References the shape object as an IShape
- public class Subscriber2
- {
- public Subscriber2(Shape shape)
- {
- IShape d = (IShape)shape;
- d.OnDraw += new EventHandler(d_OnDraw);
- }
- void d_OnDraw(object sender, EventArgs e)
- {
- Console.WriteLine("Sub2 receives the IShape event.");
- }
- }
- public class Program
- {
- static void Main(string[] args)
- {
- Shape shape = new Shape();
- Subscriber1 sub = new Subscriber1(shape);
- Subscriber2 sub2 = new Subscriber2(shape);
- shape.Draw();
- // Keep the console window open in debug mode.
- System.Console.WriteLine("Press any key to exit.");
- System.Console.ReadKey();
- }
- }
- }
- /* C#接口事件示例Output:
- Sub1 receives the IDrawingObject event.
- Drawing a shape.
- Sub2 receives the IShape event.
- */
C#接口事件的實現(xiàn)以及使用的一些內(nèi)容就向你介紹到這里,希望對你了解和學習C#接口事件有所幫助。
【編輯推薦】