Delegate Array

2019-03-26 06:02发布

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).

5条回答
Explosion°爆炸
2楼-- · 2019-03-26 06:33

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();
查看更多
SAY GOODBYE
3楼-- · 2019-03-26 06:47
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());
        }


    }

}
查看更多
闹够了就滚
4楼-- · 2019-03-26 06:53
public class MainClass
{
    static void Main()
    {
        pd[0]();
        pd[1]();
    }
}
查看更多
贪生不怕死
5楼-- · 2019-03-26 06:54

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);
          }
     }
}
查看更多
三岁会撩人
6楼-- · 2019-03-26 06:59
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");
        }
    }
}
查看更多
登录 后发表回答