Instantiating Your Types
Now that you have defined your type members it's time to instantiate your type. First, you need to define local variables to store the references of the types you are going to instantiate:
.locals init (class Sphere.Sphere,
class [mscorlib]System.IO.FileStream fs)
The preceding code creates three local variables. The first one will store a reference to the sphere object, the second one will hold a reference to an XmlSerializer and the third one will be used for a FileStream instance which has to be passed to the XmlSerializer. After creating the locals you need to instantiate the object, as follows:
newobj instance void Sphere.Sphere::.ctor(float64)
So, first you load load the arguments for the constructor call on the evaluation stack, and then you can use the statement newobj
to create a new instance of a class by specifying the constructor method which has to be called as the argument of the newobj
call. The newobj
call returns a reference to the created instance on the evaluation stack. Therefore the only thing left to do is to store the reference to a local variable using the stloc
Here's the source for the other two objects needed:
call class [mscorlib]System.Type
newobj instance void
newobj instance void
The first newobj
code block instantiates the XmlSerializer and the second one the FileStream for the target XML file.
Now that you have created all necessary objects you can call the serialize function to persist the sphere information to an XML file:
callvirt instance void
This code loads the references to the instances in the appropriate order on the evaluation stack and then uses callvirt
to call the instance method Serialize
exposed by the XmlSerializer object. The code uses callvirt
used instead of call
because the Serialize
method is overridden, and the runtime has to figure out which override to use in this call.
It is legal to call a virtual method using call, which indicates that the method address is to be resolved using the class specified by method instead of dynamically resolving the address from the object being invoked. You might use this, for example, to compile calls to methods on the statically known parent class.
In this article series, you've seen the basics of object-oriented programming with Intermediate Language Assembler. Although the samples described in this series are basic, once you get started it should be quite easy to find your way through the documentation. Take a look at the accompanying source download and the books and Web sites in the "additional resources" section to learn more about ILAsm.
In addition to the online resources listed in the left column of this article, you may also find these print resources useful.
- MSPress, Serge Lidin, Inside Microsoft .NET IL Assembler, ISBN 0-7356-1547-0. You'll find online corrections to this book here.
- Wrox, Simon Robinson, Advanced .NET Programming, ISBN 1-86100-629-2.
- Tool Developers Guide Subdirectory of your SDK Installation. See the two files Partition I Architecture.doc, and Partition III CIL.doc.
As always, you can download the source code
for DevX articles from the download link in the left-hand column.