方法 : インターフェイス イベントを実装する (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 つ以上のイベントに対する明示的なインターフェイス実装が必要です。 イベントの明示的なインターフェイス実装を記述するときは、イベント アクセサー add および remove も記述する必要があります。 通常、これらのイベント アクセサーはコンパイラによって提供されますが、この例では提供されません。

ユーザー固有のアクセサーを指定して、2 つのイベントをクラス内の単一のイベントで表すか、別々のイベントで表すかを指定できます。 たとえば、インターフェイスの仕様により、イベントを複数回発生させる必要がある場合、各イベントをクラス内の別々の実装に関連付けることができます。 次の例では、サブスクライバーで IShape または IDrawingObject の図形参照をキャストすることにより、受信する OnDraw イベントを確認します。

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();
        }
    }

}
/* Output:
    Sub1 receives the IDrawingObject event.
    Drawing a shape.
    Sub2 receives the IShape event.
*/

参照

処理手順

方法 : 派生クラスから基本クラス イベントを発生させる (C# プログラミング ガイド)

参照

イベント (C# プログラミング ガイド)

デリゲート (C# プログラミング ガイド)

明示的なインターフェイスの実装 (C# プログラミング ガイド)

概念

C# プログラミング ガイド