いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

特定のコントロールで宣言されているイベント情報の取得


typeof(X).GetEvents()メソッドを使うと、該当クラスだけでなく
継承しているクラスのイベントまで取得されます。


該当のクラスのイベント情報のみを抜き出すには、
System.Reflection.BindingFlags列挙体を指定できる方の
メソッドを利用します。


指定するフラグのビットマスクは

BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly

になります。


以下、サンプル。

using System;
using System.Reflection;
using System.Windows.Forms;

namespace MyTest{

    public class Test{

        public void Execute(){
            BindingFlags declaredOnlyEvents = (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach(EventInfo e in typeof(DataGridView).GetEvents(declaredOnlyEvents)){
                Console.WriteLine(e.Name);
            }
        }

        static void Main(){
            new Test().Execute();
        }
    }
}