Browse DevX
Sign up for e-mail newsletters from DevX


Down to the Metal: Managed Code Under the Hood (Part III) : Page 5

It's not until you get to the IL level that you see exactly how Microsoft made it possible for many languages to compile to the same runtime. In this last part of the IL Assembler series, you'll see how to write object-oriented IL code, declare fields, methods and properties and how to create object instances and access their members.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 [System.Xml] System.Xml.Serialization.XmlSerializer xs, 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:

ldc.r8 1. newobj instance void Sphere.Sphere::.ctor(float64) stloc.0

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 statement.

Here's the source for the other two objects needed:

ldtoken Sphere.Sphere call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) ldstr "DevXSample" newobj instance void [System.Xml]System.Xml.Serialization. XmlSerializer::.ctor(class [mscorlib]System.Type, string) stloc.1 ldstr "sphere.xml" ldc.i4.4 newobj instance void [mscorlib]System.IO.FileStream::.ctor(string, valuetype [mscorlib]System.IO.FileMode) stloc.2

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:

ldloc.1 ldloc.2 ldloc.0 callvirt instance void [System.Xml]System.Xml.Serialization. XmlSerializer::Serialize(class [mscorlib]System.IO.Stream, object)

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.
Electronic Documents
  • 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.

Peter Koen, a Microsoft MVP, is an independent consultant, author, and programmer. He also gives lectures at a private university in Austria on Knowledge Management. He is a certified MCP, MCAD, MCSD.NET, MCDBA, and MCT, as well as the Allied Telesyn Certifications for CAI, CASE, and IAT. Peter is the founder of the the SQL Server User group in Austria.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date