C # Custom events assigned to various delegates based on parameters

I am trying to use events to use the nice = = = syntax.

Here is my problem: I have many different events, so instead of creating many events, I would like to use only one, where the registrar additionally provides an additional parameter (string []) that describes the events (sub) that you want to listen to.

This is basically what I am doing now:

public delegate void dMethod(int index);
private Dictionary<string, dMethod> events;
public void RegisterEvents(dMethod m, string[] subEvents){
    foreach(string subEvent : subEvents){
        events[subEvent] += m;
    }
}

      

Instead, I would like to do something like this:

public delegate void dMethod(int index);
private Dictionary<string, dMethod> events;

public event dMethod Events(string[] subEvents) {
 add {
        foreach(string subEvent : subEvents){
            events[subEvent] += value;
        }
 }
 remove {
        foreach(string subEvent : subEvents){
            events[subEvent] -= value;
        }
 }
}

      

Something like passing additional parameters when registering events as soon as possible? The simplest solution I thought of was to use delegates that return a value (changing it to the string []). But this is just hacky and not worth it.

If this is not possible, is there any way to define + = - = itself?

+2


source to share


3 answers


The simplest solution I thought of was to use the return value of delegates (changing it to the [] string).

I don't quite understand what you mean by that. Will the event handler return the events it wants to handle?

I don't think what you want to do is an option. Perhaps if you ditch the .Net event system use the + = syntax by creating your own class and some implicit conversion operators to generate code like:



SomePropertyThatIsntARealEvent[new string[] {"event1", "event2"}] += someDelegate
// or even
SomePropertyThatIsntARealEvent["event1"]["event2"] += someDelegate

      

Note that even if it were doable it would be messy and hacky and it really wasn't worth it. Unfortunately, you cannot use parameters for indexers.

I would recommend that you just stick to the unkind path.

+1


source


Why don't you want to use one event per ... event?

What you are trying to do will add an indirection step (which can hurt performance), and more importantly, negate the benefits of compilation error detection and code parsing (what if your string does not match an existing event? It would be nice to know about the error during compilation rather than runtime? How about the find all references tool?)

If all of your events are separate events (which have nothing to do with each other), you must create those events. Creating one single event for all sorts of things would be like creating one big sql table with 255 columns instead of true database design.



On the other hand, you can find an abstraction of all these events and add an argument to the event handler (not to the subscription logic) specifying which sub-event was fired. See IBindingList for an example of this technique. Note the use of enumeration instead of strings to avoid misspellings.

Could you give us an example (by business) of the events you are working on?

+2


source


This is not an answer to your main question, but for ease of use I would add the "params" keyword to your method, for example:

public void RegisterEvents(dMethod m, params string[] subEvents)

      

This will allow you to call the method like this:

RegisterEvents(m, "Bob", "Doug", "Susie");

      

instead:

RegisterEvents(m, new string[] { "Bob", "Doug", "Susie" });

      

0


source







All Articles