The interface can declare events. The following example demonstrates how to implement interface events in a class. These rules are basically the same as when implementing any interface method or property.
Implement interface events in class
Declare the event in the class and then call it in the corresponding area.
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) { ShapeChanged?.Invoke(this, e); } } }
Example
The following example demonstrates how to handle less common situations: a class inherits from two or more interfaces, and each interface has the same name. In this case, you must provide an explicit interface implementation for at least one of the events. When writing explicit interface implementations for events, you must also writeadd
andremove
Event accessor. Usually these accessors are provided by the compiler, but in this case the compiler does not provide them.
By providing your own accessor, you can specify whether two events are represented by the same event in the class or by different events. For example, if events should be raised at different times according to the interface specification, each event can be associated with a separate implementation in the class. In the following example, the subscription server determines that they convert the shape reference toIShape
or IDrawingObject
Which one to receiveOnDraw
event.
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 #region IDrawingObjectOnDraw event EventHandler { add { lock (objectLock) { PreDrawEvent += value; } } remove { lock (objectLock) { PreDrawEvent -= value; } } } #endregion // Explicit interface implementation required. // Associate IShape's event with // PostDrawEvent event EventHandler { 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. PreDrawEvent?.Invoke(this, ); ("Drawing a shape."); // Raise IShape's event after the object is drawn. PostDrawEvent?.Invoke(this, ); } } public class Subscriber1 { // References the shape object as an IDrawingObject public Subscriber1(Shape shape) { IDrawingObject d = (IDrawingObject)shape; += d_OnDraw; } void d_OnDraw(object sender, EventArgs e) { ("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; } void d_OnDraw(object sender, EventArgs e) { ("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); (); // Keep the console window open in debug mode. ("Press any key to exit."); (); } } } /* Output: Sub1 receives the IDrawingObject event. Drawing a shape. Sub2 receives the IShape event. */
The above is the detailed content of how to implement interface events in C#. For more information about implementing interface events in C#, please pay attention to my other related articles!