MulticastDelegate クラスの GetInvocationList メソッドで、イベントに登録されているデリゲートのリストが取得できる。知らなかったのでメモ(. .)φメモメモ
class Program
{
public event EventHandler Sample;
public void FireSample()
{
OnSample(EventArgs.Empty);
}
protected void OnSample(EventArgs e)
{
// Sample イベントに登録されているデリゲートのリストを
// 呼び出し順に取得
Delegate[] methods = Sample.GetInvocationList();
// 逆順に呼び出す
for (int i = methods.Length - 1; 0 <= i; i--)
{
EventHandler method = (EventHandler)methods[i];
method(this, e);
}
}
static void Main(string[] args)
{
Program p = new Program();
p.Sample += delegate
{
Console.WriteLine("1番目に登録");
};
p.Sample += delegate
{
Console.WriteLine("2番目に登録");
};
p.Sample += delegate
{
Console.WriteLine("3番目に登録");
};
p.FireSample();
Console.ReadLine();
}
}
デリゲートの呼び出し順を変更したい場面がたま~にあるので、これは便利かも。