4

コードがない dll から読み込まれたコントロールからイベント ハンドラーを削除する必要があります。「公式」 (つまり、.NET Framework のパブリック メソッドでサポートされているもの) がないように見えるので、Reflection を使用してまさにそれを行ういくつかの拡張メソッドを作成することができました。

すべての詳細については、このブログ投稿を参照してください:リフレクションを使用して WinForm ListView コントロールからイベントを削除する

SelectedIndexChanged イベントを削除する方法のコード サンプルを次に示します (動的に、元のハンドラーにアクセスせずに)。

//for a UserControl (in fact any control that implements System.ComponentModel.Component)
var userControl = new UserControl();
//we can get the current mapped event handlers
userControl.eventHandlers();
//its signature
userControl.eventHandlers_MethodSignatures();
//remove one by using the static field name
userControl.remove_EventHandler("EVENT_SELECTEDINDEXCHANGED");
//or use this one specifically mapped to the SelectedIndexChanged event
userControl.remove_Event_SelectedIndexChanged

私の質問は、「別の方法はありますか?」

私のソリューションは機能し、安定しているように見えますが、.NET オブジェクトの内部操作を行っているため、(4.0 または 4.5 で) より良い解決策があるのではないでしょうか?

関連記事:

4

0 に答える 0