积极答复者
使用FlowLayoutPanel.Controls.Clear 清除了FlowLayoutPanel上的所有控件。但是上面的控件使用AddHandler绑定了自定义事件。我该在执行Clear之前。如何先RemoveHandler上面的自定义事件,以防止内存泄露。

问题
答案
-
Hi , 你可以用反射获取其handler
Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate] Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static) Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList) Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static) If controlEventInfo Is Nothing Then Throw New ArgumentException("Specified event does not exist.") End If Dim eventKey As Object = controlEventInfo.GetValue(ctrl) Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey) Return EventHandlerDelegate End Function
希望有用.Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 可推 2012年2月23日 9:25
全部回复
-
这是真的吗?资料显示AddHandler和RemoveHandler需要成对使用。否则可能会造成内存泄露。
- 已建议为答案 HUA11111335 2014年4月29日 21:01
-
Hi , 你可以用反射获取其handler
Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate] Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static) Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList) Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static) If controlEventInfo Is Nothing Then Throw New ArgumentException("Specified event does not exist.") End If Dim eventKey As Object = controlEventInfo.GetValue(ctrl) Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey) Return EventHandlerDelegate End Function
希望有用.Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 可推 2012年2月23日 9:25
-
Hi , 你可以用反射获取其handler
Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate] Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static) Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList) Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static) If controlEventInfo Is Nothing Then Throw New ArgumentException("Specified event does not exist.") End If Dim eventKey As Object = controlEventInfo.GetValue(ctrl) Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey) Return EventHandlerDelegate End Function
希望有用.
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Hi , 你可以用反射获取其handler
Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate] Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static) Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList) Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static) If controlEventInfo Is Nothing Then Throw New ArgumentException("Specified event does not exist.") End If Dim eventKey As Object = controlEventInfo.GetValue(ctrl) Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey) Return EventHandlerDelegate End Function
希望有用.
Mike Feng
很好的方法。谢谢。我现在的做法:定义一个List字段保存了每个变量。然后枚举list字段变量清除绑定。
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Hi , 你可以用反射获取其handler
Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate] Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static) Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList) Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static) If controlEventInfo Is Nothing Then Throw New ArgumentException("Specified event does not exist.") End If Dim eventKey As Object = controlEventInfo.GetValue(ctrl) Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey) Return EventHandlerDelegate End Function
希望有用.
Mike Feng
很好的方法。谢谢。我现在的做法:定义一个List字段保存了每个变量。然后枚举list字段变量清除绑定。
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.