2016-11-12 50 views
0

所以我有一個dll文件,可以生成關於它們的人員和數據。每次改變時,dll都會引發一個事件。我需要訂閱該事件,然後處理數據。如何從dll文件訂閱事件並獲取由dll生成的列表?

我對DLL

namespace PeopleGenerator 
{ 
    public class RawPeopleDataEventArgs : EventArgs 
    { 
     public RawPeopleDataEventArgs(List<string> peopleData) 
     { 
      PeopleData = peopleData; 
     } 
     public List<string> PeopleData { get; } 
    } 
    public interface IPeopleGenerator 
    { 
     event EventHandler<RawPeopleDataEventArgs> PeopleDataReady; 
    } 
} 

我也已經給出關於工廠的信息,我可以用它來得到一個IPeopleGenerator對象

namespace PeopleGenerator 
{ 
    public class PeopleGeneratorFactory 
    { 
     public static IPeopleGenerator CreatePeopleDataReceiver() 
    } 
} 

現在我試圖使以下信息訂閱級別

namespace Test 
{ 
    class EventSubscriber 
    {  
     public EventSubscriber(object o, RawPeopleDataEventArgs args) 
     { 
      List<string> listofpeople = args.PeopleData; 
      printList(listofpeople); 
     } 

     void printList(List<string> print) 
     { 
      print.ForEach(Console.WriteLine); 
      // More data processing to happen here 
     } 
    } 
} 

我的問題是我不知道如何啓動基因組從DLL的數據。與工廠類。我的想法是這樣的

static void Main(string[] args) 
{ 
    //generate a new object 
    EventSubscriber sub = new EventSubscriber(????); 
    Console.WriteLine("Press any key to exit."); 
    Console.ReadKey(); 
} 
+0

也許[this](https://msdn.microsoft.com/en-us/library/ms366768.aspx)可以幫助你 –

+0

看起來你在建築航天方面有點迷失,你從未使用過工廠功能上或訂閱該事件。請考慮[代替迭代器](https://msdn.microsoft.com/en-us/library/65zzykke(v = vs.100).aspx)。 –

回答

0

如下您訂閱的方法,而不是類,修改您的訂戶:

namespace Test 
    { 
     class EventSubscriber 
     {  
      public HandlePeople(object o, RawPeopleDataEventArgs args) 
      { 
       List<string> listofpeople = args.PeopleData; 
       printList(listofpeople); 
      } 

      void printList(List<string> print) 
      { 
       print.ForEach(Console.WriteLine); 
       // More data processing to happen here 
      } 
     } 
    } 

並利用用戶的實例(和它的方法)來處理事件:

static void Main(string[] args) 
    { 
     var recvr = PeopleGenerator.PeopleGeneratorFactory.CreatePeopleDataReceiver(); 
     var subscriber = new EventSubscriber(); 
     recvr.PeopleDataReady += new subscriber.Handle; 

     Console.WriteLine("Press any key to exit."); 
     Console.ReadKey(); 
    } 
+0

它說我需要在EventSubscriber 錯誤\t CS7036 \t立足參數沒有給定參數對應於EventSubscriber.EventSubscriber的」所需的形參「ARGS」(RawPeopleDataEventArgs) –

+0

@Morten Godrim詹森編輯答案 –