Delegate Array

2019-03-26 05:55发布

问题:

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?

public delegate void pd();

public static class MyClass
{

    static void p1()
    {
        //...
    }

    static void p2 ()
    {
        //...
    }

    //...

    static pd[] delegates = new pd[] {

        new pd( MyClass.p1 ),
        new pd( MyClass.p2)
        /* ... */
    };
}

public class MainClass
{
    static void Main()
    {
        // Call pd[0]
        // Call pd[1]
    }
}

EDIT: The reason for the array is that I need to call the delegate functions by an index as needed. They are not run in response to an event. I see a critical (stupid) error in my code as I had tried to execute the delegate function using the pd[] type rather than the name of the array (delegates).

回答1:

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


回答2:

public class MainClass
{
    static void Main()
    {
        pd[0]();
        pd[1]();
    }
}


回答3:

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


回答4:

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


回答5:

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


    }

}