And finally we can start altering assembly code. If you like remove
BeginInvoke and EndInvoke from WildCard declaration. Compile it and run it. It
still works fine. Next I think that it would be nice to have WildCard derived
straight from System.Delegate and maybe to change second parameter of .ctor from
native int to void* so that it is more like examples available from Microsoft.
Change WildCard declaration to look like this:
.class private auto ansi sealed WildCard
extends [mscorlib]System.Delegate
{
.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
.method public virtual
instance class [mscorlib]System.IAsyncResult
BeginInvoke(class [mscorlib]System.AsyncCallback
callback,
object Instance) runtime managed
{
} // end of method WildCard::BeginInvoke
.method public virtual
instance int32 EndInvoke(class [mscorlib]System.IAsyncResult
result) runtime managed
{
} // end of method WildCard::EndInvoke
} // end of class WildCard
Also change code in Main where you are calling WildCard::.ctor so that your code
looks like this: .method public hidebysig static void Main() cil managed
{
.entrypoint
// Code size 50 (0x32)
.maxstack 3
.locals ([0] int32 c,
[1] class Worker w,
[2] class WildCard v)
IL_0000: ldc.i4.2
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: newobj instance void Worker::.ctor(int32)
IL_0008: stloc.1
IL_0009: ldloc.1
IL_000a: ldftn instance int32 Worker::Multiply()
IL_0010: newobj instance void WildCard::.ctor(object,
void *)
IL_0015: stloc.2
IL_0016: ldstr "Argument is {1}\nResult
is {0}"
IL_001b: ldloc.2
IL_001c: callvirt instance int32 WildCard::Invoke()
IL_0021: box [mscorlib]System.Int32
IL_0026: ldloc.0
IL_0027: box [mscorlib]System.Int32
IL_002c: call void [mscorlib]System.Console::WriteLine(string,
object,
object)
IL_0031: ret
} // end of method User::Main
If you don’t change it that will compile but will generate run time error. Compile
it and check using debugger type of WildCard.
If you were reading “C:\Program Files\Microsoft.NET\FrameworkSDK\Tool Developers
Guide\docs\Partition II Metadata.doc” there is under 13.6 Delegates following:
"A better design would be to simply have delegate classes
derive directly from System.Delegate."
I hope that this article shows how that could be done using ILASM.
If you have any question or suggestion I can be reached at [email protected]
if time allows I will come back to you.
Exploring Delegates
- Introduction
- An Example
- Running the example
- Assembly Code
Assembly Code
You might also like...
C# books
-
Beginning ASP.NET 2.0 E-Commerce in C# 2005: From Novice to Professional
You will learn how to build a complete e-commerce web site, following the entire design and build process. The website will be constructed following industry best practices in a modular manner&emdash;with a new module introduced and completed in each...
C# forum discussion
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
Exporting data from a repeater to an Excel sheet
by TomBlack (28 replies)
-
Calculate Date of Birth from Age
by k.prashanth (7 replies)
-
Windows Service, getting outOf memory exception
by programmerdude2341 (3 replies)
-
How to Make Default Button?
by puspitaonlinebooks (24 replies)
C# podcasts
-
.NET Rocks: Stories from CodeMash!
Published 8 years ago, running time 0h56m
While at CodeMash, Carl and Richard collected some great stories. First up is Evan Huack, who digs into cudafy, a library on codeplex that allows you to run C# code (any IL code actually) on your GPU. The second story is about Jessie Shternshus and her company The Improv Effect. Jessie talks abou.
C# jobs
-
.NET, C#, MVC Senior Software Developer
Frontline Technologies, Inc. in Exton, United States
-
C#, JQuery, KnockoutJs. Nhibernate. Unique 3-6 month opportunity in Kitesurfing/Windsurfing heaven
RWI in Cabarete, Dominican Republic
$12,000-12,000 USD per year (Contract) -
C# / .NET Programmer
Mews Systems in Prague, Czech Republic
-
Experienced C# Developer
Zenco Systems Ltd in Rugby, United Kingdom
Annual Bonus, Annual Salary Review, Perfomance Related Bonus
Comments