由相同委托类型产生的每个事件是否都有自己的多播委托?

Does each event made from the same delegate type have it's own multicast delegate?

如果我有类似的东西:

    static class Program
{
    public static delegate void TestHandler();
    public static event TestHandler TestEvent;
    public static event TestHandler TestEvent1;
    public static event TestHandler TestEvent2;


    static void Main(string[] args)
    {

        TestEvent += DoSomething1;
        TestEvent1 += DoSomething2;
        TestEvent2 += DoSomething3;

        Trigger();

        Console.ReadLine();
    }

    public static void Trigger()
    {
        TestEvent();
        TestEvent1();
        TestEvent2();
    }


    private static void DoSomething3()
    {
        Console.WriteLine("Something 3 was done");
    }

    private static void DoSomething2()
    {
        Console.WriteLine("Something 2 was done");
    }

    private static void DoSomething1()
    {
        Console.WriteLine("Something 1 was done");
    }

相同类型的每个事件是否都有自己的多播委托?如何为来自相同委托类型的每个事件分隔调用列表?

Delegates 实际上是 class。因此,在您的情况下,为 TestHandler delegate.And 创建了一种类型 class。

有三个不同的实例

您可以在生成的 IL 代码中轻松看到这一点:

如您所见,有一个 TestHandler 类型,其中有三个字段 type.So 它们共享相同的类型,但它们是完全独立的...

是的,每个 event 都有自己的多播委托支持字段。事件类型(因此字段类型)相同,不会改变这一点。

代码:

public delegate void TestHandler();

static class Program
{
    public static event TestHandler TestEvent;
    public static event TestHandler TestEvent1;
    public static event TestHandler TestEvent2;
}

大致意思是:

public delegate void TestHandler();

static class Program
{
    // backing fields with the same names:
    private static TestHandler TestEvent;
    private static TestHandler TestEvent1;
    private static TestHandler TestEvent2;

    // each event is really a pair of two "methods" (accessors)
    public static event TestHandler TestEvent
    {
        add
        {
            // smart code to access the private field in a safe way,
            // combining parameter 'value' into that
        }
        remove
        {
            // smart code to access the private field in a safe way,
            // taking out parameter 'value' from that
        }
    }
    public static event TestHandler TestEvent1
    {
        add
        {
            // smart code to access the private field in a safe way,
            // combining parameter 'value' into that
        }
        remove
        {
            // smart code to access the private field in a safe way,
            // taking out parameter 'value' from that
        }
    }
    public static event TestHandler TestEvent2
    {
        add
        {
            // smart code to access the private field in a safe way,
            // combining parameter 'value' into that
        }
        remove
        {
            // smart code to access the private field in a safe way,
            // taking out parameter 'value' from that
        }
    }
}