Exploring C# Delegates


Delegates are a kind of type safe functionpointers which are actually declared as class derived fromSystem.MulticastDelegate. There are few rules how to write that class.First you must declare .ctor and Invoke methods, if you like to haveasynchronous callback involved there are two more methods just for thatcase BeginInvoke and EndInvoke.
Except for these two or four methods nothing else should be declaredand these declarations must be without implementation (empty body).This is what it looks like:

.method public hidebysig specialname rtspecialname
instance void .ctor(object ‘object’,
native int ‘method’) runtime managed
{
} // end of method WildCard::.ctor

.method public hidebysig virtual instance int32
Invoke() runtime managed
{
} // end of method WildCard::Invoke

If you like you may delete hidebysig and everything will still working.Do not delete anything else. First argument supplied to .ctor isinstance of the class that defines the target method and second ispointer to the method to be called. Following is more in conformancewith materials available from Microsoft:

.method public specialname rtspecialname
instance void .ctor(object Instance, void * Method) runtime managed

{
} // end of method WildCard::.ctor

.method public virtual instance int32
Invoke() runtime managed
{
} // end of method WildCard::Invoke

Type of Invoke must match type of method to be called, also list ofparameters and their respective types must match. BeginInvoke must beof return type System.IAsyncResult. It accepts three parameters:thefirst is the same as that being used to call Invoke, type of second isSystem.AsyncCallback and third is the same as first parameter from.ctor. EndInvoke returns and accepts the same as Invoke plus one extraparameter of type System.IAsyncResult. Now we have all the definitions.Since we are planning to use debugger create shortcut in your ?SendTo?folder and point it to “C:\ProgramFiles\Microsoft.NET\FrameworkSDK\GuiDebug\DbgCLR.exe”.
Continues…

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!