Event Declaration
In ILAsm, the syntax for an event declaration is as follows:
.event <class_ref> <name> { <method_semantics_decl>* }
where <class_ref> represents the type associated with the event, <name> is a simple name, and
<method_semantics_decl> ::= <semantics> <method_ref> <semantics> ::= .addon .removeon .fire .other
The following is an example of an event declaration:
// The delegate implementing the event .class public sealed MyEventImpl extends [mscorlib]System.MulticastDelegate { .method public hidebysig specialname void .ctor(object 'object', native int 'method') runtime managed { } .method public hidebysig virtual instance void Invoke(int32 EventCode, string Msg) runtime managed { } } // The event publisher .class public A { .field private class MyEventImpl evImpl // Aggregate delegate .method public specialname void .ctor() { ldnull ldarg.0 stfld class MyEventImpl A::evImpl ret } .method public void Subscribe(class MyEventImpl aHandler) { ldarg.0 ldfld class MyEventImpl A::evImpl ldarg.1 call class [mscorlib]System.Delegate A::Combine(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate) ldarg.0 stfld class MyEventImpl A::evImpl ret } .method public void Unsubscribe(class MyEventImpl aHandler) { ldarg.0 ldfld class MyEventImpl A::evImpl ldarg.1 call class [mscorlib]System.Delegate A::Remove(class [mscorlib]System.Delegate, class [mscorlib]System.Delegate) ldarg.0 stfld class MyEventImpl A::evImpl ret } .method public void Raise(int32 EventCode, string Msg) { ldarg.0 ldfld class MyEventImpl A::evImpl ldarg.1 ldarg.2 call void MyEventImpl::Invoke(int32, string) ret } .method public bool HasSubscribers() { ldc.i1.0 ldarg.0 ldfld class MyEventImpl A::evImpl brnull L1 pop ldc.i1.1 L1: ret } .event MyEventImpl MyEvent { .addon instance void A::Subscribe(class MyEventImpl) .removeon instance void A::Unsubscribe(class MyEventImpl) .fire instance void A::Raise(int32, string) .other instance bool A::HasSubscribers() } // Other class members } // The end of the publisher class // The event subscriber .class public B { .method public void MyEvtHandler(int32 EventCode, string Msg) { // If EventCode > 100, print the message ldarg.1 ldc.i4 100 ble.s Return ldarg.2 call void [mscorlib]System.Console::WriteLine(string) Return: ret } .method private void SubscribeToMyEvent(class A Publisher) { // Publisher->Subscribe(new MyEventImpl // (this,(int)(this->MyEvtHandler))) ldarg.1 ldarg.0 dup ldftn instance void MyEvtHandler(int32, string) newobj instance void MyEventImpl::.ctor(object, native int) call instance void A::Subscribe(class MyEventImpl) ret} // Other class members } // The end of the subscriber class