Browse DevX
Sign up for e-mail newsletters from DevX


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

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

n the first part of this article series you saw the basics of managed code execution and created your first Intermediate Language Assembler application; the well known "Hello World" sample. In the second part you saw how to perform basic programming tasks such as loops and control flow. In this part you will see how to use object oriented programming with Intermediate Assembler to declare fields, instance methods, and properties, and how to create instances and call their members. You'll also add some more metadata with attributes to your classes. This article won't cover more sophisticated topics such as delegates, as those are beyond its scope; but you can find out how to do that from the references and links in the resources column and at the end of this article.

A Sphere Example
The sample code for this article is another arithmetical project—a sphere class that you construct by providing the radius, and which offers properties for radius and volume. To make the sample a little more complex and to show off attribute usage in Intermediate Language Assembler you'll also see how to add metadata to classes and methods in ILAsm.

Extending the Manifest
Once again, you need to add some extra complexity to the manifest. In the first article you used only the assembly name, in the second you added a version number. But there's still a possibility for confusion and misuse, so now you'll add a public key token to tell the CLR that you want a signed assembly with a specific public key.

Here's the code:

Figure 1. The Global Assembly Cache: The Assembly Cache Viewer is a Windows shell extension that lets you view and manipulate the contents of the global assembly cache using Windows Explorer.

.assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 1:0:5000:0 } .assembly extern System.Xml { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 1:0:5000:0 }

You need mscorlib as the base assembly, as usual, but for this example you also need System.Xml because the sample uses the XmlSerializer class from that namespace.

You may be wondering where to get the values for the publickeytoken directives. That's simple; the key values shown are the same keys as the ones shown in the Global Assembly Cache in Windows Explorer. So, to get the key for an assembly that resides in the GAC, simply direct your Windows Explorer to <%WinDir%>\assembly\. For example, Figure 1 shows a portion of the GAC listing on my computer from C:\Windows\assembly\.

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