Note that there are some explanatory texts on larger screens.

plurals
  1. PORaise an event via reflection in c#
    primarykey
    data
    text
    <p>I want to <strong>write a reusable function</strong> to raise an event via reflection.</p> <p>After searching, I found this similar question: <a href="https://stackoverflow.com/questions/198543/how-do-i-raise-an-event-via-reflection-in-net-c">How do I raise an event via reflection in .NET/C#?</a></p> <p>It works until I register an event handler to WinForm control and try to invoke it. The private field '<code>&lt;EventName&gt;</code>' simply disappears.</p> <p>Below is my simplified code which reproduces the problem:</p> <p>Program.cs:</p> <pre><code>public static void Main() { Control control = new Control(); control.Click += new EventHandler(control_Click); MethodInfo eventInvoker = ReflectionHelper.GetEventInvoker(control, "Click"); eventInvoker.Invoke(control, new object[] {null, null}); } static void control_Click(object sender, EventArgs e) { Console.WriteLine("Clicked !!!!!!!!!!!"); } </code></pre> <p>Here is my ReflectionHelper class:</p> <pre><code>public static class ReflectionHelper { /// &lt;summary&gt; /// Gets method that will be invoked the event is raised. /// &lt;/summary&gt; /// &lt;param name="obj"&gt;Object that contains the event.&lt;/param&gt; /// &lt;param name="eventName"&gt;Event Name.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static MethodInfo GetEventInvoker(object obj, string eventName) { // --- Begin parameters checking code ----------------------------- Debug.Assert(obj != null); Debug.Assert(!string.IsNullOrEmpty(eventName)); // --- End parameters checking code ------------------------------- // prepare current processing type Type currentType = obj.GetType(); // try to get special event decleration while (true) { FieldInfo fieldInfo = currentType.GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.GetField); if (fieldInfo == null) { if (currentType.BaseType != null) { // move deeper currentType = currentType.BaseType; continue; } Debug.Fail(string.Format("Not found event named {0} in object type {1}", eventName, obj)); return null; } // found return ((MulticastDelegate)fieldInfo.GetValue(obj)).Method; } } </code></pre> <p>Additional information:</p> <ul> <li>Event in same class: worked. </li> <li>Event in different class, sub-class in same assembly: worked. </li> <li>Event in <strong>MY</strong> different assembly, debug &amp; release mode: worked. </li> <li>Event in WinForm, DevExpress, ...: did not work</li> </ul> <p>Any help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload