How are Event objects implemented in .NET?

I just want to be sure that I understand this ...

An “event” in .net is just a set of delegates, and when a class wants to receive an “Event”, it uses the “+” operator with the method the delegate points to (view of the observer)

So, if an event occurs => some pointer will walk through the collection and will call the method that was defined when the event was registered.

How do I understand this description? Is it possible to somehow see the implementation of the Microsoft.net event object?

+3
source share
3 answers

There is no object in .Net Event. Events in .Net are based on MulticastDelegates (and every delegate in .Net is MulticastDelegate).

, +, .

MulticastDelegate , , , , . , GetInvocationList() .

MulticastDelegate , , EventHandler , Reflector, , Framework.

Framework, , , SSCLI, ( MS).

+5

. . .

. get/set, add/remove ( ).

private EventHandler<EventArgs> myEvent;

public event EventHandler<EventArgs> MyEvent
{
    add
    {
        myEvent = (EventHandler<EventArgs>)Delegate.Combine(myEvent, value);
    }
    remove
    {
        myEvent = (EventHandler<EventArgs>)Delegate.Remove(myEvent, value);
    }
}

, . .

, . , , CLR. , Mono.

+3

I believe that an Event encapsulates delegates, as do relative properties and fields.

+1
source

All Articles