<bdo id='XyC6o'></bdo><ul id='XyC6o'></ul>

      <small id='XyC6o'></small><noframes id='XyC6o'>

    1. <i id='XyC6o'><tr id='XyC6o'><dt id='XyC6o'><q id='XyC6o'><span id='XyC6o'><b id='XyC6o'><form id='XyC6o'><ins id='XyC6o'></ins><ul id='XyC6o'></ul><sub id='XyC6o'></sub></form><legend id='XyC6o'></legend><bdo id='XyC6o'><pre id='XyC6o'><center id='XyC6o'></center></pre></bdo></b><th id='XyC6o'></th></span></q></dt></tr></i><div id='XyC6o'><tfoot id='XyC6o'></tfoot><dl id='XyC6o'><fieldset id='XyC6o'></fieldset></dl></div>
    2. <tfoot id='XyC6o'></tfoot>
      <legend id='XyC6o'><style id='XyC6o'><dir id='XyC6o'><q id='XyC6o'></q></dir></style></legend>

      虚拟事件如何在 C# 中工作?

      时间:2023-10-06
        <tbody id='I8cvL'></tbody>

        <small id='I8cvL'></small><noframes id='I8cvL'>

        <i id='I8cvL'><tr id='I8cvL'><dt id='I8cvL'><q id='I8cvL'><span id='I8cvL'><b id='I8cvL'><form id='I8cvL'><ins id='I8cvL'></ins><ul id='I8cvL'></ul><sub id='I8cvL'></sub></form><legend id='I8cvL'></legend><bdo id='I8cvL'><pre id='I8cvL'><center id='I8cvL'></center></pre></bdo></b><th id='I8cvL'></th></span></q></dt></tr></i><div id='I8cvL'><tfoot id='I8cvL'></tfoot><dl id='I8cvL'><fieldset id='I8cvL'></fieldset></dl></div>

        1. <tfoot id='I8cvL'></tfoot>
            • <bdo id='I8cvL'></bdo><ul id='I8cvL'></ul>
            • <legend id='I8cvL'><style id='I8cvL'><dir id='I8cvL'><q id='I8cvL'></q></dir></style></legend>
                本文介绍了虚拟事件如何在 C# 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                以下是我用于测试的程序.它打印(如预期):

                Below is the program I used for the test. It prints (as expected):

                Raise A
                Event from A
                Raise B
                Event from B
                

                现在,如果我们将 Main 的前两行更改为:

                Now, if we change first two lines of the Main to be:

                        A a = new B();
                        B b = new B();
                

                程序将打印:

                Raise A
                Raise B
                Event from B
                

                这也是意料之中的,因为覆盖事件隐藏了基类中的私有支持字段,因此基类触发的事件对派生类的客户端不可见.

                which is also expected, as overriding event hides the private backing field in the base class and therefore events fired by the base class are not visible to clients of the derived class.

                现在我将相同的行更改为:

                Now I am changing the same lines to:

                 B b = new B();
                 A a = b;
                

                程序开始打印:

                Raise A
                Raise B
                Event from A
                Event from B
                

                发生了什么事?

                class A
                {
                    public virtual event EventHandler VirtualEvent;
                    public void RaiseA()
                    {
                        Console.WriteLine("Raise A");
                        if (VirtualEvent != null)
                        {
                            VirtualEvent(this, EventArgs.Empty);
                        }
                    }
                }
                class B : A
                {
                    public override event EventHandler VirtualEvent;
                    public void RaiseB()
                    {
                        Console.WriteLine("Raise B");             
                        if (VirtualEvent != null)
                        {
                            VirtualEvent(this, EventArgs.Empty);
                        }
                    }
                }
                
                class Program
                {
                    static void Main(string[] args)
                    {
                        A a = new A();
                        B b = new B();
                
                        a.VirtualEvent += (s, e) => Console.WriteLine("Event from A");
                        b.VirtualEvent += (s, e) => Console.WriteLine("Event from B");
                
                        a.RaiseA();
                        b.RaiseB();
                    }
                }
                

                推荐答案

                我们有一个(B的)实例,它具有以下字段:

                We have a single instance (of B) which has the following fields:

                • A.VirtualEvent: null
                • B.VirtualEvent:两个事件处理程序

                a.RaiseA() 的调用 只是 打印Raise A" - 仅此而已,因为 A 中的私有字段为空.

                The call to a.RaiseA() just prints "Raise A" - but nothing more, because the private field in A is null.

                b.RaiseB() 的调用会打印剩余的三行,因为该事件已被订阅了两次(一次打印来自 A 的事件",一次打印来自 B 的事件").

                The call to b.RaiseB() prints the remaining three lines, because the event has been subscribed to twice (once to print "Event from A" and once to print "Event from B").

                这有帮助吗?

                为了更清楚 - 将虚拟事件视为一对虚拟方法.很像这样:

                To make it clearer - think of the virtual event as a pair of virtual methods. It's very much like this:

                public class A
                {
                    private EventHandler handlerA;
                
                    public virtual void AddEventHandler(EventHandler handler)
                    {
                        handlerA += handler;
                    }
                
                    public virtual void RemoveEventHandler(EventHandler handler)
                    {
                        handlerA -= handler;
                    }
                
                    // RaiseA stuff
                }
                
                public class B : A
                {
                    private EventHandler handlerB;
                
                    public override void AddEventHandler(EventHandler handler)
                    {
                        handlerB += handler;
                    }
                
                    public override void RemoveEventHandler(EventHandler handler)
                    {
                        handlerB -= handler;
                    }
                
                    // RaiseB stuff
                }
                

                现在更清楚了吗?这不是完全那样,因为据我所知,您不能只覆盖事件的部分"(即其中一种方法),但它给人的总体印象是正确的.

                Now is it clearer? It's not quite like that because as far as I'm aware you can't override just "part" of an event (i.e. one of the methods) but it gives the right general impression.

                这篇关于虚拟事件如何在 C# 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么 C# 编译器不阻止属性引用自己? 下一篇:条件运算符感到困惑,但为什么呢?

                相关文章

                <i id='4GihS'><tr id='4GihS'><dt id='4GihS'><q id='4GihS'><span id='4GihS'><b id='4GihS'><form id='4GihS'><ins id='4GihS'></ins><ul id='4GihS'></ul><sub id='4GihS'></sub></form><legend id='4GihS'></legend><bdo id='4GihS'><pre id='4GihS'><center id='4GihS'></center></pre></bdo></b><th id='4GihS'></th></span></q></dt></tr></i><div id='4GihS'><tfoot id='4GihS'></tfoot><dl id='4GihS'><fieldset id='4GihS'></fieldset></dl></div>
                1. <legend id='4GihS'><style id='4GihS'><dir id='4GihS'><q id='4GihS'></q></dir></style></legend>
                  • <bdo id='4GihS'></bdo><ul id='4GihS'></ul>
                2. <small id='4GihS'></small><noframes id='4GihS'>

                3. <tfoot id='4GihS'></tfoot>