简体   繁体   English

C#中的事件是否结构化?

[英]Are Events in C# structs?

So I have a dictionary of EventHandlers, yet I find that when I attach to an event before adding the keyvaluepair to the dictionary, everything works fine. 所以我有一个EventHandlers的字典,但我发现当我在将keyvaluepair添加到字典之前附加到一个事件时,一切正常。 But if I add the keyvaluepair and then update the value of the eventhandler, the dictionary does not update. 但是,如果我添加keyvaluepair然后更新eventhandler的值,则字典不会更新。

public static event EventHandler TestEvent;
private static Dictionary<int, EventHandler> EventMapping = new Dictionary<int, EventHandler>();

 //TestEvent += GTKWavePipeClient_TestEvent;

  EventMapping.Add(0, TestEvent);
  TestEvent += GTKWavePipeClient_TestEvent;
  //test event is non null now. keyvaluepair in EventMapping has a value of null

Delegate types like EventHandler are immutable types. EventHandler这样的委托类型是不可变类型。 When you use assignment ( = ) or compound assignment ( += ), a new instance is created. 使用赋值( = )或复合赋值( += )时,将创建一个新实例。

The dictionary holds on to the old instance. 字典保留旧实例。

Delegate types are reference types, but the important thing here is their immutability. 委托类型是引用类型,但重要的是它们的不变性。

When you have an event , the use of the += syntax is not even an assignment. 当你有一个event ,使用+=语法甚至不是一个赋值。 It is an invocation of the add accessor or of the event. 它是add accessor或event的调用。 It will reassign the backing field (new instance) in a thread-safe way. 它将以线程安全的方式重新分配支持字段(新实例)。


Remember that you can author your event accessors yourself. 请记住,您可以自己编写事件访问者。 For example: 例如:

public static event EventHandler TestEvent
{
  add
  {
    lock (lockObj)
    {
      EventHandler oldDel;
      if (EventMapping.TryGetValue(0, out oldDel))
        EventMapping[0] = oldDel + value;
      else
        EventMapping.Add(0, value);
    }
  }

  remove
  {
    lock (lockObj)
    {
      EventHandler oldDel;
      if (EventMapping.TryGetValue(0, out oldDel))
        EventMapping[0] = oldDel - value;
    }
  }
}
private static readonly object lockObj = new object();
private static Dictionary<int, EventHandler> EventMapping = new Dictionary<int, EventHandler>();

With that code, when you go: 使用该代码,当你去:

TestEvent += GTKWavePipeClient_TestEvent;

your add accessor is called with "implicit" parameter EventHandler value set to GTKWavePipeClient_TestEvent . 使用“隐式”参数EventHandler value设置为GTKWavePipeClient_TestEvent来调用您的add访问GTKWavePipeClient_TestEvent

Delegates are immutable. 代表是不可改变的。 You are assigning a new object to TestEvent when invoking += to attach an event. 在调用+ =附加事件时,您正在为TestEvent分配新对象。 So in your non-working scenario, you have a different object within the Dictionary than the object which has the attached event. 因此,在非工作场景中,Dictionary中的对象与具有附加事件的对象不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM