1. <legend id='dIlGj'><style id='dIlGj'><dir id='dIlGj'><q id='dIlGj'></q></dir></style></legend>
    2. <tfoot id='dIlGj'></tfoot>
        <bdo id='dIlGj'></bdo><ul id='dIlGj'></ul>
    3. <small id='dIlGj'></small><noframes id='dIlGj'>

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

        在 C# 中转发事件

        Forwarding events in C#(在 C# 中转发事件)
          <bdo id='rSp7M'></bdo><ul id='rSp7M'></ul>

            <legend id='rSp7M'><style id='rSp7M'><dir id='rSp7M'><q id='rSp7M'></q></dir></style></legend>
            • <tfoot id='rSp7M'></tfoot>

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

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

                  <tbody id='rSp7M'></tbody>

                  本文介绍了在 C# 中转发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用一个在 C# 中转发事件的类.我想知道是否有办法做它需要更少的代码开销.

                  I'm using a class that forwards events in C#. I was wondering if there's a way of doing it that requires less code overhead.

                  这是我到目前为止的一个例子.

                  Here's an example of what I have so far.

                  class A
                  {
                     public event EventType EventA;
                  }
                  
                  class B
                  {
                     A m_A = new A();
                     public event EventType EventB;
                  
                     public B()
                     {
                        m_A.EventA += OnEventA;
                     }
                  
                     public void OnEventA()
                     {
                        if( EventB )
                        {
                           EventB();
                        }
                     }
                  }
                  

                  A 类引发了原始事件.B 类将其作为 EventB 转发(本质上是相同的事件).A 类对其他模块是隐藏的,因此它们不能直接订阅 EventA.

                  Class A raises the original event. Class B forwards it as EventB (which is essentially the same event). Class A is hidden from other modules so they can't subscribe to EventA directly.

                  我正在尝试做的是减少 B 类中用于转发事件的代码开销,因为通常 B 类中的事件没有真正的处理.此外,我将有几个不同的事件,因此需要编写一个B 类中的许多 OnEvent() 方法仅用于转发事件.

                  What I'm trying to do is reduce the code overhead in class B for forwarding the event, as typically there's no real handling of the events in class B. Also I'll have several different events so it would require writing a lot of OnEvent() methods in class B that only serve to forward the events.

                  是否有可能以某种方式自动将 EventA 链接到 EventB,所以我有这样的东西:

                  Is it possible to automatically link EventA to EventB in some way, so I'd have something like this:

                  class B
                  {
                     A m_A = new A();
                     public event EventType EventB;
                  
                     public B()
                     {
                        m_A.EventA += EventB; // EventA automatically raises EventB.
                     }
                  }
                  

                  顺便说一句,我正在使用 C# 2.0 编译器.

                  I'm using a C# 2.0 compiler btw.

                  推荐答案

                  绝对:

                  class B
                  {
                      private A m_a = new A();
                  
                      public event EventType EventB
                      {
                          add { m_a.EventA += value; }
                          remove { m_a.EventA -= value; }
                      }
                  }
                  

                  换句话说,EventB 订阅/取消订阅代码只是将订阅/取消订阅请求传递给 EventA.

                  In other words, the EventB subscription/unsubscription code just passes the subscription/unsubscription requests on to EventA.

                  请注意,这不允许您为订阅了 EventB 的订阅者引发事件只是.这就像将某人的地址直接传递给大众营销公司,而您原来的方式更像是自己订阅大众营销公司,并允许人们要求您将邮件的副本发送给他们.

                  Note that this doesn't allow you to raise the event just for subscribers who subscribed to EventB, however. It's like passing someone's address directly onto a mass marketing company, whereas your original way is more like subscribing to the mass marketing company yourself, and allowing people to ask you to send copies of the mails to them.

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C# namespace alias - what#39;s the point?(C# 命名空间别名 - 有什么意义?)
                  Using Xpath With Default Namespace in C#(在 C# 中使用具有默认命名空间的 Xpath)
                  Generating an EDMX from a DB2 Database(从 DB2 数据库生成 EDMX)
                  IBM .NET Data Provider Connection String issue with Library List(库列表的 IBM .NET 数据提供程序连接字符串问题)
                  .NET DB2 OLEDB pre-requisites(.NET DB2 OLEDB 先决条件)
                  Referring to Code in IBM.Data.DB2 makes that Assembly Unavailable to the rest of my Solution(引用 IBM.Data.DB2 中的代码使该程序集对我的解决方案的其余部分不可用)
                  <tfoot id='6wlzx'></tfoot>

                      <tbody id='6wlzx'></tbody>

                        <small id='6wlzx'></small><noframes id='6wlzx'>

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

                          1. <legend id='6wlzx'><style id='6wlzx'><dir id='6wlzx'><q id='6wlzx'></q></dir></style></legend>