代表阵列 [英] Delegate Array
问题描述
我从一个委托调用数组功能委托试验。我已经能够创建代表的数组,但如何调用该委托?
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]
}
}
编辑:的原因阵列是我需要通过一个索引根据需要调用该委托功能。它们不响应于一个事件运行。我看到我的code关键(愚蠢)误差作为我曾试图用PD []类型,而不是阵列(代表)的名称来执行委托功能。
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).
推荐答案
如果他们都是同一类型的,为什么不将它们组合成一个单一的多播委托?
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();
这篇关于代表阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!