Events of a non-delegate type

前端 未结 4 413
一生所求
一生所求 2021-01-03 03:27

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

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


        
相关标签:
4条回答
  • 2021-01-03 04:00

    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.

    0 讨论(0)
  • 2021-01-03 04:04

    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.

    0 讨论(0)
  • 2021-01-03 04:09

    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.

    0 讨论(0)
  • 2021-01-03 04:13

    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 the + 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.

    0 讨论(0)
提交回复
热议问题