logo
CSharp_Prog_Guide

Реализация событий интерфейса в классе

-----

Example

The following example shows how to handle the less-common situation in which your class inherits from two or more interfaces and each interface has an event with the same name. In this situation, you must provide an explicit interface implementation for at least one of the events. When you write an explicit interface implementation for an event, you must also write the add and remove event accessors. Normally these are provided by the compiler, but in this case the compiler cannot provide them.

By providing your own accessors, you can specify whether the two events are represented by the same event in your class, or by different events. For example, if the events should be raised at different times according to the interface specifications, you can associate each event with a separate implementation in your class. In the following example, subscribers determine which OnDraw event they will receive by casting the shape reference to either an IShape or an IDrawingObject.

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;

}