Array of events in C #?

mostly

public delegate void RecvCommandHandler (ChatApplication sender, byte[] content); event RecvCommandHandler[] commands = new RecvCommandHandler[255]; 

I want to activate a different method / function for each command number, but I'm really not sure about the syntax. How am I supposed to do this?

I think that for this I will take only an array of delegates, but the question is still interesting.

+4
source share
4 answers

Actually, there is no concept of a multitude of events - this is similar to a story about an array of properties. Events are really just methods that allow you to subscribe and unsubscribe handlers. If you need to do this by index, I offer you just a couple of methods. ( AddCommandHandler(int, RecvCommandHandler) and RemoveCommandHandler(int, RecvCommandHandler) ). Of course, this will not support the normal handling of syntactic sugar processing, but I do not see that there are many alternatives.

+7
source

You can create an array of a class with operator overloading to mimic the behavior that interests you ...

 public delegate void EventDelegate(EventData kEvent); public class EventElement { protected event EventDelegate eventdelegate; public void Dispatch(EventData kEvent) { if (eventdelegate != null) { eventdelegate(kEvent); } } public static EventElement operator +(EventElement kElement, EventDelegate kDelegate) { kElement.eventdelegate += kDelegate; return kElement; } public static EventElement operator -(EventElement kElement, EventDelegate kDelegate) { kElement.eventdelegate -= kDelegate; return kElement; } } public EventElement[] commands = new EventElement[255]; commands[100] += OnWhatever; commands[100].Dispatch(new EventData()); commands[100] -= OnWhatever; 
+7
source

Another option is to specify and index the delegate in the prototype and have one event handler that "delegates" to other users, for example:

 public delegate void RecvCommandHandler (int id, ChatApplication sender, byte[] content); // ... private RecvCommandHandler[] internalhandlers; public void MyCommandHandler(int id, ChatApplication sender, byte[] content) { internalHandlers[id](id, sender, content); } 
+2
source

I was just looking for the same answer, however my class is also an event sender for WPF, so it should look just like a regular C # / WPF event sender class. So I just added this:

To sender:

  • enum with property name is a lame workaround due to missing name
  • one additional method for recording requests

To the recipient:

  • event request for this listing

Code, sender:

  public enum Properties { NetworkFileName, DatasetFileName, LearningWatch } private string network_filename; public string NetworkFileName { get { return network_filename; } private set { if (network_filename != value) { network_filename = value; OnPropertyChanged(Properties.NetworkFileName.ToString()); } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } public void OnChange(Properties prop, Action<object, PropertyChangedEventArgs> action) { PropertyChanged += new PropertyChangedEventHandler((obj, args) => { if (args.PropertyName == prop.ToString()) action(obj, args); }); } 

And to the recipient:

  private void OnNetworkLoaded(object sender, PropertyChangedEventArgs e) { SetTitle(); } ... ExpManager.OnChange(ExperimentManager.Properties.DatasetFileName, OnDatasetLoaded); 

This is still ugly, but at least:

  • I do not need to deal with "ifs" in the receiver
  • I can easily create multiple event handlers
  • It is compatible with WPF
  • no magic lines (I hate them)

Inconvenience:

  • obsfuscation destroys this (but I have a special class for this case, this project is only for me, so there is no problem here)
0
source

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


All Articles