2011-03-07 50 views
1

爲了避免try-catch塊,有沒有辦法確定控件是否支持指定的事件而沒有try-catch塊?使用反射來確定控件是否支持事件

Dim d As [Delegate] = [Delegate].CreateDelegate(eventHandler.EventHandlerType, _ 
                   Me, _ 
                   "OnControlValueChanged") '<< 

回答

2

使用反射:

Dim events As System.Reflection.EventInfo() = GetType(Control).GetEvents() 
For Each someEvent As System.Reflection.EventInfo In events 
    If someEvent.Name = "OnControlValueChanged" Then 
     'Do what you need to do 
    End If 
Next