HOW TO:以事件名称与方法名称为参数的事件关联

Author:水如烟

使用代码示例:

Public   Class  Form1

    
Private   Sub  box1TextChanged( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)
        Send(
Me .TextBox1.Text)
    
End Sub

    
Private   Sub  Send( ByVal  s  As   String )
        TestClass.Receive(s)
    
End Sub

    
Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        Common.AddEventHandler(
" TextChanged " Me .TextBox1,  " box1TextChanged " Me )
    
End Sub

    
Private   Sub  Button2_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button2.Click
        Common.RemoveEventHandler(
" TextChanged " Me .TextBox1,  " box1TextChanged " Me )
    
End Sub

    
Private   Sub  Button3_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button3.Click
        Common.AddEventHandler(
" MyEvent " GetType (TestClass),  " ShowMessage " GetType (TestClass))
    
End Sub

    
Private   Sub  Button4_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button4.Click
        Common.RemoveEventHandler(
" MyEvent " GetType (TestClass),  " ShowMessage " GetType (TestClass))
    
End Sub

End Class

类:

Imports  System.Reflection
Namespace  uReflection
    
Public   Class  Common
        
Private   Sub   New ()
        
End Sub
        
' '' <summary>
         ' '' 添加事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在实例</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在实例</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  AddEventHandler( ByVal  eventName  As   String ByVal  target  As   Object ByVal  methodName  As   String ByVal  instance  As   Object )
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
' 取事件信息
             Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetType.GetEvent(eventName, mBinding)
            
' 取方法信息
             Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetType.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            
If  mMethodInfo.IsStatic  Then
                
' 静态方法的委托申请
                d  =  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)
            
Else
                
' 实例方法的委托申请
                d  =  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType, instance, mMethodInfo)
            
End   If

            
'移除
            mEventInfo.AddEventHandler(target, d)
        
End Sub

        
' '' <summary>
         ' '' 添加事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在实例</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在静态类</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  AddEventHandler( ByVal  eventName  As   String ByVal  target  As   Object ByVal  methodName  As   String ByVal  instance  As  Type)
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetType.GetEvent(eventName, mBinding)
            
Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)

            mEventInfo.AddEventHandler(target, d)
        
End Sub

        
' '' <summary>
         ' '' 添加事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在静态类</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在实例</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  AddEventHandler( ByVal  eventName  As   String ByVal  target  As  Type,  ByVal  methodName  As   String ByVal  instance  As   Object )
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetEvent(eventName, mBinding)
            
Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetType.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            
If  mMethodInfo.IsStatic  Then
                d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)
            
Else
                d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType, instance, mMethodInfo)
            
End   If

            mEventInfo.AddEventHandler(target, d)
        
End Sub

        
' '' <summary>
         ' '' 添加事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在静态类</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在静态类</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  AddEventHandler( ByVal  eventName  As   String ByVal  target  As  Type,  ByVal  methodName  As   String ByVal  instance  As  Type)
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetEvent(eventName, mBinding)
            
Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)

            mEventInfo.AddEventHandler(target, d)
        
End Sub


        
' '' <summary>
         ' '' 移除事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在实例</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在实例</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  RemoveEventHandler( ByVal  eventName  As   String ByVal  target  As   Object ByVal  methodName  As   String ByVal  instance  As   Object )
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
' 取事件信息
             Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetType.GetEvent(eventName, mBinding)
            
' 取方法信息
             Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetType.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            
If  mMethodInfo.IsStatic  Then
                
' 静态方法的委托申请
                d  =  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)
            
Else
                
' 实例方法的委托申请
                d  =  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType, instance, mMethodInfo)
            
End   If

            
' 绑定
            mEventInfo.RemoveEventHandler(target, d)
        
End Sub

        
' '' <summary>
         ' '' 移除事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在实例</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在静态类</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  RemoveEventHandler( ByVal  eventName  As   String ByVal  target  As   Object ByVal  methodName  As   String ByVal  instance  As  Type)
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetType.GetEvent(eventName, mBinding)
            
Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)

            mEventInfo.RemoveEventHandler(target, d)
        
End Sub

        
' '' <summary>
         ' '' 移除事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在静态类</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在实例</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  RemoveEventHandler( ByVal  eventName  As   String ByVal  target  As  Type,  ByVal  methodName  As   String ByVal  instance  As   Object )
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetEvent(eventName, mBinding)
            
Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetType.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            
If  mMethodInfo.IsStatic  Then
                d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)
            
Else
                d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType, instance, mMethodInfo)
            
End   If

            mEventInfo.RemoveEventHandler(target, d)
        
End Sub

        
' '' <summary>
         ' '' 移除事件绑定
         ' '' </summary>
         ' '' <param name="eventName">事件名称</param>
         ' '' <param name="target">事件所在静态类</param>
         ' '' <param name="methodName">方法名称</param>
         ' '' <param name="instance">方法所在静态类</param>
         ' '' <remarks></remarks>
         Public   Shared   Sub  RemoveEventHandler( ByVal  eventName  As   String ByVal  target  As  Type,  ByVal  methodName  As   String ByVal  instance  As  Type)
            
Dim  mBinding  As  Reflection.BindingFlags  =  Reflection.BindingFlags.Instance  Or  Reflection.BindingFlags.Public  Or  Reflection.BindingFlags.NonPublic  Or  BindingFlags.Static

            
Dim  mEventInfo  As  Reflection.EventInfo  =  target.GetEvent(eventName, mBinding)
            
Dim  mMethodInfo  As  Reflection.MethodInfo  =  instance.GetMethod(methodName, mBinding)

            
Dim  d  As  [ Delegate ]
            d 
=  [ Delegate ].CreateDelegate(mEventInfo.EventHandlerType,  Nothing , mMethodInfo)

            mEventInfo.RemoveEventHandler(target, d)
        
End Sub
    
End Class
End Namespace

效果
HOW TO:以事件名称与方法名称为参数的事件关联

Demo下载

 

你可能感兴趣的:(方法)