英語で読む

次の方法で共有


EventHandler<TEventArgs> 代理人

定義

イベントがデータを提供するときにイベントを処理するメソッドを表します。

C#
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
C#
public delegate void EventHandler<TEventArgs>(object? sender, TEventArgs e);
C#
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
C#
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);

型パラメーター

TEventArgs

イベントによって生成されるイベント データの型。

パラメーター

sender
Object

イベントのソース。

e
TEventArgs

イベント データを格納しているオブジェクト。

属性

次の例は、 という名前 ThresholdReachedのイベントを示しています。 イベントはデリゲートに EventHandler<TEventArgs> 関連付けられています。

C#
using System;

namespace ConsoleApplication3
{
    public class Program3
    {
        public static void Main()
        {
            Counter c = new(new Random().Next(10));
            c.ThresholdReached += c_ThresholdReached;

            Console.WriteLine("press 'a' key to increase total");
            while (Console.ReadKey(true).KeyChar == 'a')
            {
                Console.WriteLine("adding one");
                c.Add(1);
            }
        }

        static void c_ThresholdReached(object? sender, ThresholdReachedEventArgs e)
        {
            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold,  e.TimeReached);
            Environment.Exit(0);
        }
    }

    class Counter
    {
        private readonly int _threshold;
        private int _total;

        public Counter(int passedThreshold)
        {
            _threshold = passedThreshold;
        }

        public void Add(int x)
        {
            _total += x;
            if (_total >= _threshold)
            {
                ThresholdReachedEventArgs args = new()
                {
                    Threshold = _threshold,
                    TimeReached = DateTime.Now
                };
                OnThresholdReached(args);
            }
        }

        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            ThresholdReached?.Invoke(this, e);
        }

        public event EventHandler<ThresholdReachedEventArgs>? ThresholdReached;
    }

    public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }
}

注釈

.NET Frameworkのイベント モデルは、イベントをハンドラーに接続するイベント デリゲートを持つことに基づいています。 イベントを発生させるためには、次の 2 つの要素が必要です。

  • イベントへの応答を提供するメソッドを参照するデリゲート。

  • 必要に応じて、イベントがデータを提供する場合は、イベント データを保持するクラス。

デリゲートは、シグネチャ、つまりメソッドの戻り値の型とパラメーター リスト型を定義する型です。 デリゲート型を使用して、デリゲートと同じシグネチャを持つ任意のメソッドを参照できる変数を宣言できます。

イベント ハンドラー デリゲートの標準シグネチャは、値を返さないメソッドを定義します。 このメソッドの最初のパラメーターは 型 Object であり、 イベントを発生させるインスタンスを参照します。 2 番目のパラメーターは 型 EventArgs から派生し、イベント データを保持します。 イベントがイベント データを生成しない場合、2 番目のパラメーターはフィールドの EventArgs.Empty 値です。 それ以外の場合、2 番目のパラメーターは から EventArgs 派生した型であり、イベント データを保持するために必要なフィールドまたはプロパティを提供します。

デリゲートは EventHandler<TEventArgs> 、データを生成するイベントのイベント ハンドラー メソッドを表す定義済みのデリゲートです。 を使用 EventHandler<TEventArgs> する利点は、イベントによってイベント データが生成される場合に、独自のカスタム デリゲートをコーディングする必要がないようにすることです。 イベント データ オブジェクトの型をジェネリック パラメーターとして指定するだけです。

イベントをイベントを処理するメソッドに関連付けるには、デリゲートのインスタンスを イベントに追加します。 イベント ハンドラーは、デリゲートを削除しない限り、イベントが発生するたびに呼び出されます。

イベント ハンドラー デリゲートの詳細については、「イベントの 処理と発生」を参照してください。

拡張メソッド

GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください