Is it possible to subscribe to event subscriptions in C #?

If I have an event like this:

public delegate void MyEventHandler(object sender, EventArgs e); public event MyEventHandler MyEvent; 

And adds an event handler as follows:

 MyEvent += MyEventHandlerMethod; 

... is it possible to somehow register this? In other words - is it possible to have something like:

 MyEvent.OnSubscribe += MySubscriptionHandler; 
+6
source share
5 answers

Like automatically implemented properties, events are also automatically executed by default.

You can expand the event declaration as follows:

 public event MyEventHandler MyEvent { add { ... } remove { ... } } 

See, for example, How to Use a Dictionary to Store Event Instances (C # Programming Guide)

See Events a bit redesigned in C # 4, part I: Locks for how auto-injection events differ between C # and nbsp; 3 and C # 4.

+11
source

It is possible to declare event accessors specifically, i.e. add and remove accessors.

This allows you to do custom logic when adding new event handlers.

+6
source

When you define your events, you can use a longer format to execute more code when people join or remove from your events.

View information on add and remove keywords.

+1
source

Perhaps if you declare your custom event, for example, this pseudocode:

 class MyClass { public event EventHandler MyEvent { add { //someone subscribed to this event ! } remove { //someone unsubscribed from this event ! } } ... } 
+1
source

I assume that you are looking for access to events. A way to configure subscriber links. Here is how you can do it.

 public class TestClass { private event EventHandler UnderlyingEvent; public event EventHandler TestEvent { add { UnderlyingEvent += value; } remove { UnderlyingEvent -= value; } } } 

For more information, please visit this article.

http://msdn.microsoft.com/en-us/magazine/cc163533.aspx

+1
source

Source: https://habr.com/ru/post/915094/


All Articles