EventHandler<TEventArgs> 代理人
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
イベントがデータを提供するときにイベントを処理するメソッドを表します。
generic <typename TEventArgs>
public delegate void EventHandler(System::Object ^ sender, TEventArgs e);
generic <typename TEventArgs>
where TEventArgs : EventArgspublic delegate void EventHandler(System::Object ^ sender, TEventArgs e);
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
public delegate void EventHandler<in TEventArgs>(object? sender, TEventArgs e) where TEventArgs : allows ref struct;
public delegate void EventHandler<TEventArgs>(object? sender, TEventArgs e);
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
type EventHandler<'EventArgs> = delegate of obj * 'EventArgs -> unit
[<System.Serializable>]
type EventHandler<'EventArgs (requires 'EventArgs :> EventArgs)> = delegate of obj * 'EventArgs -> unit
[<System.Serializable>]
type EventHandler<'EventArgs> = delegate of obj * 'EventArgs -> unit
Public Delegate Sub EventHandler(Of TEventArgs)(sender As Object, e As TEventArgs)
Public Delegate Sub EventHandler(Of In TEventArgs)(sender As Object, e As TEventArgs)
型パラメーター
- TEventArgs
イベントによって生成されるイベント データの型。
パラメーター
- sender
- Object
イベントのソース。
- e
- TEventArgs
イベント データを格納するオブジェクト。
- 属性
例
次の例は、 ThresholdReachedという名前のイベントを示しています。 イベントは、 EventHandler<TEventArgs> デリゲートに関連付けられています。
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; }
}
}
open System
type ThresholdReachedEventArgs(threshold, timeReached) =
inherit EventArgs()
member _.Threshold = threshold
member _.TimeReached = timeReached
type Counter(threshold) =
let mutable total = 0
let thresholdReached = Event<_>()
member this.Add(x) =
total <- total + x
if total >= threshold then
let args = ThresholdReachedEventArgs(threshold, DateTime.Now)
thresholdReached.Trigger(this, args)
[<CLIEvent>]
member _.ThresholdReached = thresholdReached.Publish
let c_ThresholdReached(sender, e: ThresholdReachedEventArgs) =
printfn $"The threshold of {e.Threshold} was reached at {e.TimeReached}."
exit 0
let c = Counter(Random().Next 10)
c.ThresholdReached.Add c_ThresholdReached
printfn "press 'a' key to increase total"
while Console.ReadKey(true).KeyChar = 'a' do
printfn "adding one"
c.Add 1
Module Module1
Sub Main()
Dim c As Counter = New Counter(New Random().Next(10))
AddHandler c.ThresholdReached, AddressOf c_ThresholdReached
Console.WriteLine("press 'a' key to increase total")
While Console.ReadKey(True).KeyChar = "a"
Console.WriteLine("adding one")
c.Add(1)
End While
End Sub
Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs)
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached)
Environment.Exit(0)
End Sub
End Module
Class Counter
Private threshold As Integer
Private total As Integer
Public Sub New(passedThreshold As Integer)
threshold = passedThreshold
End Sub
Public Sub Add(x As Integer)
total = total + x
If (total >= threshold) Then
Dim args As ThresholdReachedEventArgs = New ThresholdReachedEventArgs()
args.Threshold = threshold
args.TimeReached = DateTime.Now
OnThresholdReached(args)
End If
End Sub
Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs)
RaiseEvent ThresholdReached(Me, e)
End Sub
Public Event ThresholdReached As EventHandler(Of ThresholdReachedEventArgs)
End Class
Class ThresholdReachedEventArgs
Inherits EventArgs
Public Property Threshold As Integer
Public Property TimeReached As DateTime
End Class
注釈
.NETのイベント モデルは、イベントをハンドラーに接続するイベント デリゲートを持つことに基づいています。 イベントを発生させるためには、次の 2 つの要素が必要です。
- イベントへの応答を提供するメソッドを参照するデリゲート。
- 必要に応じて、イベントデータを保持するクラス (イベントがデータを提供する場合)。
デリゲートは、シグネチャ、つまりメソッドの戻り値の型とパラメーター リスト型を定義する型です。 デリゲート型を使用して、デリゲートと同じシグネチャを持つ任意のメソッドを参照できる変数を宣言できます。
イベント ハンドラー デリゲートの標準シグネチャは、値を返さないメソッドを定義します。 このメソッドの最初のパラメーターは Object 型であり、イベントを発生させるインスタンスを参照します。 その 2 番目のパラメーターは、 EventArgs 型から派生し、イベント データを保持します。 イベントがイベント データを生成しない場合、2 番目のパラメーターは単に EventArgs.Empty フィールドの値です。 それ以外の場合、2 番目のパラメーターは EventArgs から派生した型であり、イベント データを保持するために必要なフィールドまたはプロパティを提供します。
EventHandler<TEventArgs> デリゲートは、データを生成するイベントのイベント ハンドラー メソッドを表す定義済みのデリゲートです。 EventHandler<TEventArgs>を使用する利点は、イベントがイベント データを生成する場合に独自のカスタム デリゲートをコーディングする必要がないようにすることです。 イベント データ オブジェクトの型をジェネリック パラメーターとして指定するだけです。
イベントをイベントを処理するメソッドに関連付けるには、デリゲートのインスタンスをイベントに追加します。 デリゲートを削除しない限り、イベントが発生するたびにイベント ハンドラーが呼び出されます。
イベント ハンドラー デリゲートの詳細については、「イベントの 処理と発生」を参照してください。
拡張メソッド
| 名前 | 説明 |
|---|---|
| GetMethodInfo(Delegate) |
指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。 |