C# - Create an EventHandler that can take any number of parameters

后端 未结 3 1463
孤街浪徒
孤街浪徒 2021-02-08 03:07

I wish to create a custom EventHandler that can have any number of objects as its parameters and the objects it gets isn\'t known in advance.

I know I can pass it an Obj

3条回答
  •  不要未来只要你来
    2021-02-08 03:52

    You can define a delegate as such:

    public delegate void MyHandler(object p1, object p2, object p3);
    

    and then use it in your event definition:

    public event MyHandler MyEvent;
    

    However, this is contrary to best practices and not recommended. Instead, you should encapsulate all the extra information you require into your own EventArgs subclass and use that from your delegate:

    public class MyEventArgs : EventArgs
    {
        // any extra info you need can be defined as properties in this class
    }
    
    public event EventHandler MyEvent;
    

提交回复
热议问题