Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does event handler registeration happen?

Tags:

c#

.net

We often use event handler in C#, just as below:

some_event+=some_event_handler;

Does this happen at compile time or run time? What if the some_event is static member? AFAIK, the some_event contains nothing but the entry address of some_event_handler, and the method address of some_event_handler could be determined at compile time. If some_event is a static member, could the value of the some_event be determined at compile time? I konw that if the some_event is an instance member, it's value will be setup at the creation time of the object instance. Correct me if I'm wrong.

Many thanks, guys~ :)

like image 378
smwikipedia Avatar asked Jun 30 '10 07:06

smwikipedia


1 Answers

We can say it concerns delegates. A delegate variable is assigned a method dynamically. This gives us ability to write plugin methods.

Concerning instance and static method targets just a note from C# in Nutshell

When a delegate object is assigned to an instance method, the delegate object must maintain a reference not only to the method, but also to the instance to which the method belongs. The System.Delegate class’s Target property represents this instance (and will be null for a delegate referencing a static method).

like image 82
Incognito Avatar answered Oct 19 '22 15:10

Incognito