I was wondering about the EventInfo.GetRaiseMethod
and EventInfo.GetOtherMethods
methods. Apparently, the CLR supports 4 kinds of methods associated with events: add, remove, raise, and "others". But events created in C# only have add and remove... I assumed that raise was used in VB, since you have to specify a RaiseEvent
method when you declare a custom event, but apparently it's not the case: GetRaiseMethod
always returns null.
So, does anyone know:
GetOtherMethods
? What's are they supposed to do?As far as I know, raise isn't used much, and I've practically never seen it used. C++/CLI is pretty much the only language I know that make it easy to declare a raise method. See this code for example:
using namespace System;
ref class Foo {
private:
Action ^bar;
public:
event Action ^Bar {
void add (Action ^action)
{
Console::WriteLine ("add");
bar += action;
}
void remove (Action ^action)
{
Console::WriteLine ("remove");
bar -= action;
}
void raise ()
{
Console::WriteLine ("raise");
if (!bar)
return;
Console::WriteLine ("raise for real");
bar->Invoke ();
}
};
};
void hello ()
{
Console::WriteLine ("hello");
}
void main ()
{
Foo ^foo = gcnew Foo ();
foo->Bar ();
foo->Bar += gcnew Action (&hello);
foo->Bar ();
}
Which, when being run, naturally outputs:
C:\tmp>test
raise
add
raise
raise for real
hello
To answer your question, there's no opcode to invoke an event, the compiler will just emit a call to the raise method:
IL_0020: ldloc.0
IL_0021: call instance void Foo::raise_Bar()
Just like it emits a call to add_Bar.
It's also worth nothing that as C# allows you to invoke an event exclusively in the scope of the type which declares the member event, you can't get C# code to call that raise method. So no, you won't find such a method exposed in the BCL.
As for the .other
kind of methods, I've never seen any attached to an event. And I only saw them used once for properties, and neither the book «Inside IL assembler» nor «The CLI annotated standard» give any information about them. But basically, they allow you to attach methods to a property or to a event to bind them semantically. They're neither a addon
, nor a removeon
, nor a raise
method, but they would be part of the event, should a language need to express that. In the meantime, the only way to emit one is to use ilasm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With