views:

432

answers:

4

I've implemented a class that looks like this interface:

[ImmutableObject(true)]
public interface ICustomEvent
{
    void Invoke(object sender, EventArgs e);

    ICustomEvent Combine(EventHandler handler);
    ICustomEvent Remove(EventHandler handler);

    ICustomEvent Combine(ICustomEvent other);
    ICustomEvent Remove(ICustomEvent other);
}

This CustomEvent class works much like a MulticastDelegate. It can invoked. It can be combined with another CustomEvent. And a CustomEvent can be removed from another CustomEvent.

Now, I want to declare a class like this:

class EventProvider
{
    public event CustomEvent MyEvent;

    private void OnMyEvent()
    {
     var myEvent = this.MyEvent;
     if (myEvent != null) myEvent.Invoke(this, EventArgs.Empty);
    }
}

Unfortunately, this code does not compile. A Compiler Error CS0066 appears:

'EventProvider.MyEvent': event must be of a delegate type

Basically, what I need is a property that has add and remove accessors instead of get and set. I think the only way to have that is using the event keyword. I know that one obvious alternative is to declare two methods that would do the adding and removing, but I want to avoid that too.

Does anybody knows if there is a nice solution this problem? I wonder if there is any way to cheat the compiler to accept a non-delegate type as an event. A custom attribute, perhaps.

By the way, someone asked a similar question in experts-exchange.com. Since that site is not free, I can't see the responses. Here is the topic: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21697455.html

EDITED at 2009.07.16 13:01 UTC: ICustomEvent changed

+1  A: 

What are you trying to accomplish that you can't use delegates/events for?

This screams Reinventing the Square Wheel (bottom of the page), but that could just as well be me not understanding the problem.

280Z28
I would tend to agree. If you need to perform tasks on the adding and removing of event handlers, use the `add` and `remove` accessors, which you mention.
Noldorin
One of the possibilities is a easier way to implement a transparent Weak Event Pattern.
jpbochi
Perhaps, but then you have code that violates everything you already know about [.NET events and weak events][1], which devastates code readability even though you're *trying* to improve it. [1]: http://msdn.microsoft.com/en-us/library/aa970850.aspx
280Z28
+3  A: 

Try this:

CustomEvent myEvent

public event EventHandler MyEvent {
    add { myEvent = myEvent.Combine(value); }
    remove {myEvent = myEvent.Remove(value); }
}

You can add and remove normal EventHandler delegates to it, and it will execute the add and remove accessors.


EDIT: You can find a weak event implementation here.
2nd EDIT: Or here.

SLaks
1st) Thanks for your suggestion, but it's 100% exactly to a regular event. That's not what I'm looking for;2nd) I want to raise the event from the EventProvider class. I don't think raising events from external classes is a good idea in any situation;3rd) Thanks for your links, but I already knew both of them. My code, by the way, is strongly inspired by the codeproject article you mentioned.
jpbochi
If the first one isn't what you want to do, what do you want? <br>To be able to add `ICustomEvent`s?
SLaks
I'm sorry, I misread your answer. You're right, I want to be able to add and remove ICustomEvent, not only EventHandler's. I fixed the question to make it clearer. (I would vote this answer up if I had enough reputation)
jpbochi
+1  A: 

If you want to be able to add and remove CustomEvent objects from the event (instead of regular delegates), there are two options:

Make an implicit cast from ICustomEvent to EventHandler (or some other delegate) that returns an instance method of ICustomEvent (probably Invoke), then use the Target property of the delegate to get the original ICustomEvent in the add and remove accessors.

EDIT: Like this:

CustomEvent myEvent;
public event EventHandler MyEvent {
    add {
        if (value == null) throw new ArgumentNullException("value");
        var customHandler = value.Target as ICustomEvent;

        if (customHandler != null)
            myEvent = myEvent.Combine(customHandler);
        else
            myEvent = myEvent.Combine(value);   //An ordinary delegate
    }
    remove {
        //Similar code
    }
}

Note that you'll still need to figure out how to add the first handler if it's a delegate (if the myEvent field is null)


Make a writable property of type CustomEvent, then overload add + and - operators to allow += and -= on the property.

EDIT: To prevent your callers from overwriting the event, you could expose the previous value in CustomEvent (I'm assuming it works like an immutable stack) and, in the setter, add

if (myEvent.Previous != value && value.Previous != myEvent)
    throw new ArgumentException("You cannot reset a CustomEvent", "value");

Note that when the last handler is removed, both value and myEvent.Previous will be null.

SLaks
About the implicit cast option: it's an interesting idea. I see that it's possible to hide a CustomEvent inside a delegate. Maybe, I could mark the method with a custom attribute in order to differentiate a CustomEvent from a Delegate. I'll try to code it. thanks!
jpbochi
About the writable property option: I have considered it in the past, but I dislike it for one good reason. Any external code would be able to overwrite the current CustomEvent. I want a solution that provides the same pattern that regular events do: user code is only able to add/remove the handlers it knows. Invoking the event and reading the current handlers should not be allowed.
jpbochi
@1st: What would be the point of a custom attribute? I don't see what you're saying there.
SLaks
@2nd: See edit.
SLaks
@Konrad: Link doesn't work.
John Saunders
You need to view Google's cache of the page. Search Google for cache:_<any ExpertsExchange Url>_, or use Google Toolbar's view cache button
SLaks
@1st: A nice-to-have feature is being able to implicitly convert a EventHandler to a CustomEvent. Using a delegate typed event, I could have the 2 options. The added value could be either an common EventHandler or a CustomEvent disguised as an EventHandler. To distinguish between the two, I could check if the delegate Method has some custom attribute. I'm starting to liek this solution.
jpbochi
@2nd: Indeed, this can work too. Now, I don't know what option to pursue first. I guess I'll be forced to accept your answer. :)
jpbochi
@1st: You don't need to check for an attribute - you can simply check whether the Target property is an instance of ICustomEvent, like this: `if (value.Target is ICustomEvent)`
SLaks
See edit.
SLaks
@1st: You're right, I don't need attributes. I'll just ask one correction. A null value is acceptable, but has to be ignored. That's how regular events work. So, if you substitute the *throw new ArgumentNullException("value");* for a *return;* I accept your answer. Thank you very much. :)
jpbochi
A: 

Why don't you try to use the "+=" and the "-=" operators on your CustomEvent class? You cannot override the "+=" and the "-=" operators directly, but they are evaluated by "+" and "-" operator.

Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded.

http://msdn.microsoft.com/en-us/library/8edha89s(VS.90).aspx

So instead of having event-like add and remove methods, you can have a field or a property that can be combined by += and -= operators. Besides it encapsulates the combination logic inside your own CustomEvent class.

Carlos Loth.

Carlos Loth
That's the same as the second option that SLaks proposed above. He also proposed a special treatment to avoid the possibility of overwriting the event. To be honest, I prefer his first option. I have already coded it and I'm doing some tests.
jpbochi
I have to agree with you, the first option is better because you acctualy can use "regular" event handlers in addition to your custom weak event. Besides, I haven't noticed his comment about overloading the + and - operators. Sorry.
Carlos Loth