Matt Mitchell Matt Mitchell - 1 year ago 164
C# Question

Get event parameters via reflection

I can't work out how to get the parameter types for an event.

For instance, I can only see using a MethodInfo to get parameters, but I have either an EventInfo or a FieldInfo.

What I want is to be able to get 'Boolean' from this:

Public Event EventName(ByVal sender As Object, ByVal value As Boolean)

I could theoretically try something like GetRaiseMethod() but that won't work (because that method returns null as per this link) and even if it did it would require a method binding first and this is meant to be for a test suite just confirming that the event has a certain typed parameter at initialisation.

Any ideas?

Answer Source

You can get the type of the second parameter as follows assuming the event EventName is declared in the class DeclaringClass and the event has at least to parameters. Else you will probably receive an exception.

Type secondEventHandlerParameterType = 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download