Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#事件连接在处理程序中工作';s对象';仅限s构造函数_C#_Events_Event Handling - Fatal编程技术网

C#事件连接在处理程序中工作';s对象';仅限s构造函数

C#事件连接在处理程序中工作';s对象';仅限s构造函数,c#,events,event-handling,C#,Events,Event Handling,我正在尝试将接口.MessageReceived连接到记录器.LogReceivedMessage。如果我加上 interface.MessageReceived+=LogReceivedMessage; 在logger的构造函数中,按预期调用了LogReceivedMessage 但是,如果我将连接移动到logger中的其他位置,则永远不会调用LogReceivedMessage 到接口的所有其他连接。MessageReceived使用+=而不是=,因此这不是问题所在 我使用了visualst

我正在尝试将
接口.MessageReceived
连接到
记录器.LogReceivedMessage
。如果我加上 interface.MessageReceived+=LogReceivedMessage; 在
logger
的构造函数中,按预期调用了
LogReceivedMessage

但是,如果我将连接移动到
logger
中的其他位置,则永远不会调用
LogReceivedMessage

接口的所有其他连接。MessageReceived
使用
+=
而不是
=
,因此这不是问题所在

我使用了visualstudio中的Watch窗口和“makeobjectid”选项来验证代码是否在两个位置都有相同的
接口实例。是的。但是,
interface.MessageReceived
在构造函数中的对象与代码中的其他位置不同。事实上,每当有东西连接到它时,它似乎都会改变。我不确定这是否是预期的

有人知道为什么我只能在构造函数中连接处理程序吗

编辑: 我已经让它工作了,但我不知道它为什么工作。
界面中的原始代码
类:

public event Action<Message> MessageReceived;
busClient.MessageReceived += MessageReceived
收到公共事件动作消息;
busClient.MessageReceived+=MessageReceived
我把它改成:

public event Action<Message> intermediateMessageReceived;
public event Action<Message> MessageReceived;
busClient.MessageReceived += intermediateMessageReceived;

public void intermediateMessageReceived(Message m)
{
   MessageReceived(m);
}
收到公共事件行动中间消息;
收到公共事件行动信息;
busClient.MessageReceived+=中间消息已接收;
已接收公共无效中间消息(消息m)
{
收到的信息(m);
}

在没有将所有代码发布到整个项目中的情况下,有人知道为什么会有不同的行为吗?

我想与大家分享我在C#中了解到的与此问题相关的事件。我们不了解事件是如何运作的,这导致了我们的问题

当您将事件B订阅到事件A时,调用事件A将仅将事件B发生时订阅到事件B的内容调用到订阅。如果B的订阅服务器发生更改,则在调用事件A时不会反映该更改

下面的代码演示:

namespace EventDemonstrator
{
   class Program
   {
      class BottomLayer
      {
         public event System.Action<string> Event;
        public void callEvent() { Event("bottom"); }
      }
      class MiddleLayer
      {
         public void HookUpEvent(BottomLayer bl) { bl.Event += this.Event; }
         public event System.Action<string> Event;
      }
      class TopLayer
      {
         public void TopLayerHandler(string s)
         {
            System.Console.Write(string.Format(" {0} top\n", s));
         }
      }

      static void HookBottomToMiddle_ThenMiddleToTop_ThenCallBottom(BottomLayer bottom, MiddleLayer middle, TopLayer top)
      {
         middle.HookUpEvent(bottom);
         middle.Event += top.TopLayerHandler;
         try {bottom.callEvent(); }
         catch (System.NullReferenceException) { System.Console.Write("HookBottomToMiddle_ThenMiddleToTop_ThenCallBottom: Event was null\n"); }
      }

      static void HookMiddleToTop_ThenBottomToMiddle_ThenCallBottom(BottomLayer bottom, MiddleLayer middle, TopLayer top)
      {
         middle.Event += top.TopLayerHandler;
         middle.HookUpEvent(bottom);
         System.Console.Write("HookMiddleToTop_ThenBottomToMiddle_ThenCallBottom:");
         bottom.callEvent();
      }

      static void Main(string[] args)
      {
         HookBottomToMiddle_ThenMiddleToTop_ThenCallBottom(new BottomLayer(), new MiddleLayer(), new TopLayer());
         HookMiddleToTop_ThenBottomToMiddle_ThenCallBottom(new BottomLayer(), new MiddleLayer(), new TopLayer());
      }
   }
}

听起来像个虫子。我不认为任何人都能回答这个问题,请在这个表单中发布您的代码。嗯。。您是否有一个继承的MessageReceived,您正在使用自己的私有/受保护的MessageReceived隐藏,即不应用override关键字?
HookBottomToMiddle_ThenMiddleToTop_ThenCallBottom: Event was null
HookMiddleToTop_ThenBottomToMiddle_ThenCallBottom: bottom top