Note that there are some explanatory texts on larger screens.

plurals
  1. PORaise event through a general method
    primarykey
    data
    text
    <p>I'm trying to create a reusable method for a more complicated event execution. I can't get it to compile or run with framework events that don't follow the <code>EventHandler&lt;Type&gt;</code> pattern. I would like to avoid reflection if possible as it will be a heavily used event.</p> <p>I've created a test console app below which illustrates the problem:</p> <pre><code>using System; using System.Collections.Specialized; namespace CallEventsViaMethod { public class TestEventArgs : EventArgs { } class Program { static void Main(string[] args) { MyProgram program = new MyProgram(); program.Go(); Console.ReadKey(false); } } public class MyProgram { public event EventHandler&lt;TestEventArgs&gt; TestEvent; public event NotifyCollectionChangedEventHandler CollectionChangedEvent; public void Go() { TestEvent += new EventHandler&lt;TestEventArgs&gt;(MyProgram_TestEvent); CollectionChangedEvent += new NotifyCollectionChangedEventHandler(MyProgram_CollectionChangedEvent); // Want a reusable method I can use to conditionally execute any event GeneralEventExecutor.Execute&lt;TestEventArgs&gt;(TestEvent, new Object(), new TestEventArgs()); GeneralEventExecutor.Execute&lt;NotifyCollectionChangedEventArgs&gt;(TestEvent, new Object(), new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } void MyProgram_TestEvent(object arg1, TestEventArgs arg2) { Console.WriteLine("Custom event ran"); } void MyProgram_CollectionChangedEvent(object sender, NotifyCollectionChangedEventArgs e) { Console.WriteLine("NotifyCollectionChangedEventHandler event ran"); } } public static class GeneralEventExecutor { public static void Execute&lt;T&gt;(EventHandler&lt;T&gt; eventToRaise, object sender, T eventArgs) where T : EventArgs { if (eventToRaise == null) return; Delegate[] registeredEventHandlers = eventToRaise.GetInvocationList(); foreach (EventHandler&lt;T&gt; eventHandler in registeredEventHandlers) { object target = eventHandler.Target; // Need access to the Target property // * Code deciding if should invoke the event handler * eventHandler.Invoke(sender, eventArgs); } } } } </code></pre> <p>Error messages are:</p> <blockquote> <p>error CS1502: The best overloaded method match for 'CallEventsViaMethod.GeneralEventExecutor.Execute(System.EventHandler, object, System.Collections.Specialized.NotifyCollectionChangedEventArgs)' has some invalid arguments</p> <p>error CS1503: Argument 1: cannot convert from 'System.EventHandler' to 'System.EventHandler'</p> </blockquote> <p>I understand why I'm getting the error, but can't figure out a way round it.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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