Delegate Array in C#

前端 未结 5 1039
一整个雨季
一整个雨季 2021-02-14 18:10

I am experimenting with calling delegate functions from a delegate array. I\'ve been able to create the array of delegates, but how do I call the delegate?

publ         


        
相关标签:
5条回答
  • 2021-02-14 18:26

    If they're all the same type, why not just combine them into a single multicast delegate?

    static pd delegateInstance = new pd(MyClass.p1) + new pd(MyClass.p2) ...;
    
    ...
    pd();
    
    0 讨论(0)
  • 2021-02-14 18:27

    In .Net, any delegate is in fact actually a "multicast" delegate (it inherits from this built-in base class), and therefore contains an internal linked list which can contain any number of target delegates.

    You can access this list by calling the method GetInvocationList() on the delegate itself. This method returns an array of Delegates...

    The only restriction is that all the delegates inside of a given delegate's linked list must have the same signature, (be of the same delegate type). If you need your collection to be able to contain delegates of disparate types, then you need to construct your own list or collection class.

    But if this is ok, then you can "call" the delegates in a given delegate's invocation list like this:

    public delegate void MessageArrivedHandler(MessageBase msg);
    public class MyClass
    {
         public event MessageArrivedHandler MessageArrivedClientHandler;   
    
         public void CallEachDelegate(MessageBase msg)
         {
              if (MessageArrivedClientHandler == null)
                  return;
              Delegate[] clientList = MessageArrivedClientHandler.GetInvocationList();
              foreach (Delegate d in clientList)
              {
                  if (d is MessageArrivedHandler)
                      (d as MessageArrivedHandler)(msg);
              }
         }
    }
    
    0 讨论(0)
  • 2021-02-14 18:30
    public class MainClass
    {
        static void Main()
        {
            pd[0]();
            pd[1]();
        }
    }
    
    0 讨论(0)
  • 2021-02-14 18:35
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pd[0](1);
            pd[1](2);
        }
    
        public delegate void delegates(int par);
        static delegates[] pd = new delegates[] 
                                         { 
                                          new delegates(MyClass.p1), 
                                          new delegates(MyClass.p2) 
                                         };
        public static class MyClass
        {
    
            public static void p1(int par)
            {
                MessageBox.Show(par.ToString());
            }
    
            public static void p2(int par)
            {
                MessageBox.Show(par.ToString());
            }
    
    
        }
    
    }
    
    0 讨论(0)
  • 2021-02-14 18:41
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pd[0]();
            pd[1]();
        }
    
        public delegate void delegates();
    
        static delegates[] pd = new delegates[] 
                                { 
                                   new delegates(MyClass.p1), 
                                   new delegates(MyClass.p2) 
                                };
    
        public static class MyClass
        {
            public static void p1()
            {
                MessageBox.Show("1");
            }
    
            public static void p2()
            {
                MessageBox.Show("2");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题