I realise there are a number of questions out there which deal with raising an event via reflection, however I haven't been able to find an answer to the following [I suspect the answer is "no"]:
Given a "standard" declaration of an event, is there any way to raise the event by reference to a string literal.
For example, pseudocode:
Dim eventName As String = "TestEvent"
RaiseEvent eventName
Obviously that won't work.
I can get the type of the event handler / multicast delegate with
Me.GetType.GetEvent("TestEvent").GetAddMethod.GetParameters(0).Name
// "TestEventEventHandler
But I can't find the instance of this on the page object to call .GetInvocationList
This is similar to this question: How can I get an actual EventHandler delegate instance from an event in VB.NET?
However here I'm specifically looking at raising an event from a string.
Edit:
A couple of things are different in the vb.net / webforms environment. As per my comment to the accepted answer, due to (I believe) the nature of the code-behind model, it's not possible to return the field corresponding to the event from Me.GetType()
, as during runtime Me
refers to the inheriting class in the .aspx file rather than the class in the .aspx.vb file.
In effect this means I have to use Me.BaseType.GetType()
to find the field.
The second thing which is different, though not related to the final answer, is that while in c# you can directly refer to the event handler MulticastDelegate, in vb.net you can't - or at least, to do so you have to use an undocumented feature unsupported by intellisense, as per: How can I get an actual EventHandler delegate instance from an event in VB.NET?