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

      • <bdo id='EPFrk'></bdo><ul id='EPFrk'></ul>
    1. <small id='EPFrk'></small><noframes id='EPFrk'>

      <tfoot id='EPFrk'></tfoot>

        <legend id='EPFrk'><style id='EPFrk'><dir id='EPFrk'><q id='EPFrk'></q></dir></style></legend>

        在 C# 中,为什么我不能测试事件处理程序在它定义的类之外的任何地方是否为空?

        In C#, why can#39;t I test if a event handler is null anywhere outside of the class that it#39;s defined?(在 C# 中,为什么我不能测试事件处理程序在它定义的类之外的任何地方是否为空?)

        <small id='9BXZX'></small><noframes id='9BXZX'>

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

                  <tbody id='9BXZX'></tbody>

                • 本文介绍了在 C# 中,为什么我不能测试事件处理程序在它定义的类之外的任何地方是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我确信我只是不了解有关 C# 中事件和/或委托的基本知识,但为什么我不能在此代码示例中进行布尔测试:

                  I am sure that I am just not understanding something fundamental about events and/or delegates in C#, but why can't I do the Boolean tests in this code sample:

                  public class UseSomeEventBase {
                      public delegate void SomeEventHandler(object sender, EventArgs e);
                      public event SomeEventHandler SomeEvent;
                      protected void OnSomeEvent(EventArgs e) {
                          // CANONICAL WAY TO TEST EVENT. OF COURSE, THIS WORKS.
                          if (SomeEvent != null) SomeEvent(this, e);
                      }
                  }
                  
                  public class UseSomeEvent : UseSomeEventBase {
                      public bool IsSomeEventHandlerNull() {
                          // "LEFT HAND SIDE" COMPILER ERROR
                          return SomeEvent == null;
                      }
                  }
                  
                  class Program {
                      static void Main(string[] args) {
                          var useSomeEvent = new UseSomeEvent();
                          useSomeEvent.SomeEvent +=new UseSomeEventBase.SomeEventHandler(FuncToHandle);
                          // "LEFT HAND SIDE" COMPILER ERROR
                          if (useSomeEvent.SomeEvent == null) {
                  
                          }
                          var useSomeEventBase = new UseSomeEventBase();
                          useSomeEventBase.SomeEvent += new UseSomeEventBase.SomeEventHandler(FuncToHandle);
                          // "LEFT HAND SIDE" COMPILER ERROR
                          if (useSomeEventBase.SomeEvent == null) {
                  
                          }
                      }
                  
                      static void FuncToHandle(object sender, EventArgs e) { }
                  }
                  

                  推荐答案

                  事件实际上只是一个添加"操作和一个删除"操作.您无法获取该值,您无法设置该值,您无法调用它 - 您可以订阅事件的处理程序 (add) 或取消订阅 (remove).这很好——它是封装,简单明了.发布者可以适当地实施添加/删除,但除非发布者选择提供详细信息,否则订阅者无法修改或访问特定于实施的部分.

                  An event is really just an "add" operation and a "remove" operation. You can't get the value, you can't set the value, you can't call it - you can just subscribe a handler for the event (add) or unsubscribe one (remove). This is fine - it's encapsulation, plain and simple. It's up to the publisher to implement add/remove appropriately, but unless the publisher chooses to make the details available, subscribers can't modify or access the implementation-specific parts.

                  C# 中的类字段事件(您没有指定添加/删除位)隐藏了这一点 - 它们创建了一个委托类型的变量一个事件.事件的添加/删除实现只是使用变量来跟踪订阅者.

                  Field-like events in C# (where you don't specify the add/remove bits) hide this - they create a variable of a delegate type and an event. The event's add/remove implementations just use the variable to keep track of the subscribers.

                  在类内部您引用变量(因此您可以获取当前订阅的委托,执行它们等),在类外部您引用事件本身(因此只有添加/删除功能).

                  Inside the class you refer to the variable (so you can get the currently subscribed delegates, execute them etc) and outside the class you refer to the event itself (so only have add/remove abilities).

                  类字段事件的替代方法是您自己显式实现添加/删除,例如

                  The alternative to field-like events is where you explicitly implement the add/remove yourself, e.g.

                  private EventHandler clickHandler; // Normal private field
                  
                  public event EventHandler Click
                  {
                      add
                      {
                          Console.WriteLine("New subscriber");
                          clickHandler += value;
                      }
                      remove
                      {
                          Console.WriteLine("Lost a subscriber");
                          clickHandler -= value;
                      }
                  }
                  

                  有关详细信息,请参阅我关于事件的文章.

                  See my article on events for more information.

                  当然,事件发布者可以提供更多信息——你可以写一个像ClickHandlers这样的属性来返回当前的多播委托,或者HasClickHandlers返回是否有.不过,这不是核心事件模型的一部分.

                  Of course the event publisher can also make more information available - you could write a property like ClickHandlers to return the current multi-cast delegate, or HasClickHandlersto return whether there are any or not. That's not part of the core event model though.

                  这篇关于在 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 中的代码使该程序集对我的解决方案的其余部分不可用)
                          <tbody id='LpwWH'></tbody>

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

                        1. <legend id='LpwWH'><style id='LpwWH'><dir id='LpwWH'><q id='LpwWH'></q></dir></style></legend>
                          • <bdo id='LpwWH'></bdo><ul id='LpwWH'></ul>
                            <tfoot id='LpwWH'></tfoot>

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