动态删除事件处理程序的解决方案(使用reflection)。 有一个更好的方法吗?

我需要从一个我没有代码的dll加载的控件中删除事件处理程序。 由于似乎没有“官方”(即由.NET Framework的公共方法支持),我能够使用Reflection创建一些完全相同的扩展方法。

有关所有详细信息,请参阅此博客文章: 使用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中)?

相关文章:

  • 如何删除Button的Click事件的所有事件处理程序? – 使用与我类似的解决方案,但我不认为他们的解决方案有效