2013-05-26 53 views
0

我正在嘗試爲更復雜的事件執行創建可重用的方法。我無法編譯或運行不符合EventHandler<Type>模式的框架事件。如果可能的話,我想避免反思,因爲這將是一個大量使用的事件。通過一般方法提高事件

我已經創建了一個測試控制檯應用程序下面示出了此問題:

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<TestEventArgs> TestEvent; 
     public event NotifyCollectionChangedEventHandler CollectionChangedEvent; 

     public void Go() 
     { 
      TestEvent += new EventHandler<TestEventArgs>(MyProgram_TestEvent); 
      CollectionChangedEvent += new NotifyCollectionChangedEventHandler(MyProgram_CollectionChangedEvent); 

      // Want a reusable method I can use to conditionally execute any event 
      GeneralEventExecutor.Execute<TestEventArgs>(TestEvent, new Object(), new TestEventArgs()); 
      GeneralEventExecutor.Execute<NotifyCollectionChangedEventArgs>(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<T>(EventHandler<T> eventToRaise, object sender, T eventArgs) where T : EventArgs 
     { 
      if (eventToRaise == null) 
       return; 

      Delegate[] registeredEventHandlers = eventToRaise.GetInvocationList(); 

      foreach (EventHandler<T> 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); 
      } 
     } 
    } 
} 

錯誤信息是:

錯誤CS1502:用於 「CallEventsViaMethod.GeneralEventExecutor最好重載方法匹配。執行(System.EventHandler, 對象, System.Collections.Specialized.NotifyCollectionChangedEventArgs)'具有 一些無效參數

錯誤CS1503:參數1:不能轉換從 「System.EventHandler」到 「System.EventHandler」

我明白爲什麼我得到的錯誤,但不能圓想出一個辦法它。

+0

你不reinveinting事件聚合? –

+0

Execute方法在生產代碼中的作用是正常調用事件,或者通過調度程序調用它,我只是試圖簡化代碼。在這種情況下,我不認爲Event Aggregator足夠了嗎? – Lee

+0

看起來它不能沒有反射:http://stackoverflow.com/questions/8407886/pass-event-as-parameter-to-a-method – Lee

回答

0

更換你的通用Execute<T>如下

public static void Execute<T>(Delegate eventToRaise, object sender, T eventArgs) where T:EventArgs 
     { 
      if (eventToRaise == null) 
       return; 

      Delegate[] registeredEventHandlers = eventToRaise.GetInvocationList(); 

      foreach (Delegate eventHandler in registeredEventHandlers) 
      { 
       object target = eventHandler.Target; // Need access to the Target property for conditions 

       // * Code deciding if should invoke the event handler * 

       eventHandler.DynamicInvoke(sender, eventArgs); 
      } 
     } 
+0

在運行時在線eventHandler.Invoke獲取錯誤... 「Delegate'System.EventHandler '有一些無效參數」 – Lee

+0

與上面的DynamicInvoke一起檢查 – Nair

+0

不明白爲什麼,但我得到:'System.Collections.Specialized.NotifyCollectionChangedEventArgs'類型的對象不能轉換爲類型'CallEventsViaMethod.TestEventArgs'。 – Lee