简体   繁体   English

事件词典

[英]Dictionary of events

Is there anything in c# that you can use to create a collection of events like lists, hashsets, and a dictionary in this case? 在这种情况下,c#中是否有可用于创建列表,哈希集和字典等事件的集合? Thankyou. 谢谢。

Sure you can: 你当然可以:

Dictionary<String, Delegate> m_events = new Dictionary<String, Delegate>();

In Jeff Richter's Book CLR via C# you can find a complete implementation of an EventSet class. 在Jeff Richter的Book CLR中,通过C#,您可以找到EventSet类的完整实现。

(Even if you don't have the book you can download the sample code from https://www.wintellect.com/wp-content/uploads/2017/09/CLR-via-C-4th-Edition-Code.zip . The class in question is in "Ch11-1-EventSet.cs".) (即使您没有该书,也可以从https://www.wintellect.com/wp-content/uploads/2017/09/CLR-via-C-4th-Edition-Code.zip下载示例代码。有问题的课程在“Ch11-1-EventSet.cs”中。)

As far as the question goes what an event is: 至于问题是什么,事件是什么:

Say you have this line of code in your class: 假设你的班级有这行代码:

public event EventHandler<NewMailEventArgs> NewMail;

then the C# compiler will translate the line above into three constructs: 那么C#编译器会将上面的行转换为三个结构:

  1. A private delegate field. 私人代表字段。 (Something like: private EventHandler<NewMailEventArgs> NewMail; (例如: private EventHandler<NewMailEventArgs> NewMail;

  2. A public add_Xxx method (where Xxx is the Event name) that interested subscribers can use to register a callback delegate with the event. 一个公共add_Xxx方法(其中Xxx是事件名称),感兴趣的订阅者可以使用该方法向事件注册回调委托。

  3. A public remove_Xxx method (where Xxx is the Event name) that subscribers can use to unregister from the event. 公共remove_Xxx方法(其中Xxx是事件名称),订阅者可以使用该方法从事件中取消注册。

(All the gory details can be found in section 11.2 of aforementioned book.) (所有血腥细节都可以在上述书的第11.2节中找到。)

So in other words the 'thing' you can store in your Event collection class is just a plain old delegate nothing else. 换句话说,你可以存储在Event集合类中的'thing'只是一个普通的旧委托。

An event is like a property or method; 事件就像财产或方法; you cant store instances of them. 你不能存储它们的实例。 You can use reflection, but this likely isn't what you want. 你可以使用反射,但这可能不是你想要的。

Did you mean you want to store a list of methods to call when your event is fired? 您是否想要存储在事件被触发时要调用的方法列表? Events already do that. 事件已经这样做了。

EDIT: 编辑:

Ah, I think I get it now. 啊,我想我现在明白了。 If you just simply fire your event, only the B instances that want it will get it. 如果您只是简单地触发事件,那么只有想要它的B实例才能获得它。 If you pass into the event args the instance of A that fired the event, then the B instance will be able to tell where it came from. 如果你向事件args传递了触发事件的A实例,那么B实例将能够告诉它来自何处。 But a B will never get an event from an A that it didn't ask for an event. 但是B将永远不会从A中获得它没有要求事件的事件。

EDIT: 编辑:

Maybe not. 也许不吧。 You only want to fire an event on one of the B's? 你只想在其中一个B上发射一个事件? Make B implement an interface that has a callback method. 使B实现具有回调方法的接口。 Instead of firing an event, call the method on the correct instance of B. 不是触发事件,而是在正确的B实例上调用该方法。

You could use IList with out any trouble. 你可以使用IList没有任何麻烦。 Type T could be any type defined in .net or user defined class. 类型T可以是.net或用户定义的类中定义的任何类型。

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

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