Typeクラスには型がデリゲート型かどうかを調べるIsDelegate
のようなプロパティは用意されていません。 Typeがデリゲート型を表すかどうかを調べたい場合は以下のような方法をとることができます。
Typeクラスを使って型がデリゲート型かどうかを調べる
using System;
class Sample {
static bool IsDelegate(Type t)
{
// 型がDelegate型またはDelegate型の派生クラスの場合はデリゲート型と判定する
return t.IsSubclassOf(typeof(Delegate)) || t.Equals(typeof(Delegate));
}
static void Main()
{
foreach (var t in new[] {
typeof(EventHandler),
typeof(Action<,>),
typeof(Delegate),
typeof(MulticastDelegate),
typeof(ICloneable),
}) {
Console.WriteLine("IsDelegate({0}) = {1}", t, IsDelegate(t));
}
}
}
実行結果
IsDelegate(System.EventHandler) = True IsDelegate(System.Action`2[T1,T2]) = True IsDelegate(System.Delegate) = True IsDelegate(System.MulticastDelegate) = True IsDelegate(System.ICloneable) = False