By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,221 Members | 1,295 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,221 IT Pros & Developers. It's quick & easy.

Reflection: Executing event code via reflection

P: n/a
Hi everyone,

I've been trying to execute a control's private event code via reflection
from another class with the goal of potentially doing some unit testing. The
examples below are trying to execute button2's click event.

This works great when i know the name of the method that i want to invoke. I
do so by doing this:

Dim AssemblyPointer As Reflection.Assembly
AssemblyPointer = Reflection.Assembly.LoadFile(Application.StartupPa th &
Dim mytype As Type = AssemblyPointer.GetType("WindowsApplication1.Form2 ")
Dim OneFormInstance As Form = Activator.CreateInstance(mytype)

Dim OneMethodInfo As Reflection.MethodInfo =
mytype.GetMethod("Button2_Click", Reflection.BindingFlags.NonPublic Or
If Not OneMethodInfo Is Nothing Then
Dim arguments As Object()
ReDim arguments(1)
arguments(0) = OneFormInstance
arguments(1) = New EventArgs
Reflection.BindingFlags.InvokeMethod Or Reflection.BindingFlags.NonPublic Or
Reflection.BindingFlags.Instance, mytype.DefaultBinder, OneFormInstance,
End If
I want to be able to do the same thing without knowing the name of the
method beforhand in case it ever changed.... I've tried

Dim OnePropertyInfo As Reflection.PropertyInfo =
mytype.GetProperty("Button2", Reflection.BindingFlags.NonPublic Or
If Not OnePropertyInfo Is Nothing Then
myButton = OnePropertyInfo.GetValue(OneFormInstance, Nothing)
Dim clickEvent As Reflection.EventInfo =
myButton.GetType.GetEvent("Click", Reflection.BindingFlags.Public Or
'********* At this point GetRaiseMethod is always nothing. If it returned a
methodInfo i could invoke it as above
end if

Any ideas or alternate schools of thought?


Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.