如果我只向委托注册一个类的一个方法,我可以使用delegate.Target
,但是当我从不同的类订阅更多方法时,这不再起作用。还有其他方法可以访问此代表的订阅者列表吗?
这是代码:foreach 循环在运行时被评估为 null(它编译)
public delegate void WriteMessage(string msg);
internal class Program
{
private static void Main(string[] args)
{
var myClass = new MyClass();
var writer = new WriteMessage(myClass.WriteMessage);
writer += SaySomething; //method in this class
writer += myClass.SayShit; //instance class
writer += AnotherClass.Say; //static class
foreach(string target in (string[])writer.Target)
{
Console.WriteLine(target);
}
Console.ReadLine();
}
private static void SaySomething(string msg)
{
Console.WriteLine("HI!!!!");
}
}
完整代码: http: //pastebin.com/AzzRGMY9