Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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.


advertisement
Specifying Assembly Details
Now that you have finished the necessary imports, to complete the manifest you need to specify the details for the assembly in which you want your class to reside.

.assembly Sphere { .hash algorithm 0x00008004 .ver 1:0:0:0 } .module Sphere.exe .subsystem 0x00000003

This code looks pretty much like the sample code from the last article; only the .hash directive is new. The .hash directive specifies the algorithm that the framework uses to compute the cryptographic hash used to sign this assembly. The value shown (0x8004) is the default value and represents the SHA1 algorithm, which all CLI compliant implementations must use. Microsoft chose SHA1 as the best widely available technology at the time of standardization. A single algorithm is the most practical solution because all conforming implementations of the CLI are required to implement all algorithms to ensure the portability of executable images. Note that all other values are currently reserved for future use.

Adding a Namespace and the Class Declaration
Now that you have finished the manifest you can add a namespace and a forward declaration for the classes in that namespace. You could add also the members of the class here, although it's more convenient to first declare all classes and then to add the implementation of their members afterward.

Editor's Note. No Line-Continuation Character in IL: Throughout this article, you'll see lines of code that (as in the following code fragment) should appear on a single line; however, due to the limited display space available in most browsers, we were forced to wrap them. Wherever that occurs, we indented the wrapped lines to help make it clear that—when you write or copy the IL code yourself—you should put the code on a single line.

.namespace Sphere { .class public auto ansi serializable beforefieldinit Sphere extends [mscorlib]System.Object { } }

The preceding code looks pretty much like C#, although you will notice a few differences: As always with ILAsm, all directives start with a dot (a period). The namespace declaration is simply a container declaration as in most high-level languages and—like C-style languages—uses curly brackets to define the scope—and therefore the members—of that namespace.

The .class directive is a bit more complicated, here's a description of the keywords specified here:

  • public—This keyword has the same meaning as in most high-level languages: The class shall be visible to everybody.
  • auto—The framework should generate the memory layout of field members automatically
  • ansi—Marshal all strings that need to be marshalled to the platform as ANSI type strings.
  • serializable—Specifies that this type can be serialized. You need the serializable attribute because of the XmlSerializer that you'll add later). Note that creating a serializable class in most high level .NET languages requires the SerializableAttribute.
  • beforefieldinit—This keyword specifies that calling a static method will not initialize the type, it instructs the CLI that it need not call a constructor of the type before calling a static method.
  • Sphere—The name of the sample class. In this particular case, it's the same as the namespace name, but that's just a coincidence.
  • extends—This is the keyword for inheritance. You follow it by a list of classes and interfaces that this type extends.
  • [mscorlib]System.Object—This is the base type that the current type extends. You could extend some other type, but every CLI compliant type must extend this base type.


Comment and Contribute

 

 

 

 

 


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

 

 

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