Events of a non-delegate type

前端 未结 4 416
一生所求
一生所求 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: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.

提交回复
热议问题