简体   繁体   English

如何从dll文件订阅事件并获取dll生成的列表?

[英]How to subscribe to a event from a dll file and get a list generated by the dll?

So I have a dll file that generates people and data about them. 因此,我有一个dll文件,该文件生成人员和有关人员的数据。 Every time something changes the dll raises an event. 每次更改dll都会引发一个事件。 I need to subscribe to the event and then process the data. 我需要订阅事件,然后处理数据。

I have the following information about the 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; 
    }
} 

I have also been given info about a factory I can use to get an IPeopleGenerator object 我还获得了有关我可以用来获取IPeopleGenerator对象的工厂的信息

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

Now I have tried to make a subscription class 现在我尝试制作一个订阅类

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
        }
    }
}

My problem is I cant't figure out how to start generating the data from the dll. 我的问题是我不知道如何开始从dll生成数据。 with the factory class. 与工厂班级。 My thought is something like this 我的想法是这样的

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

You subscribe methods, not classes, modify your subscriber as following: 您订阅方法而不是类,请按以下方式修改订户:

 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
            }
        }
    }  

And use instance of subscriber (and its method) to handle events: 并使用订户实例(及其方法)来处理事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM