By now you've heard a lot of talk about how .NET's Common Language Runtime (CLR) promises to let developers write code in numerous languages and then assemble them for use in the .NET framework. (I covered the basics of this approach in an earlier article.) Now let's take a closer look at how developers can use the CLR to accomplish these goals. We'll also discuss how the System.Reflection interface in .NET's CLR is used to discover information about .NET objects, regardless of whether documentation on those objects exists. Finally, we dissect what happens during the runtime execution of a CLR-compliant object.
Code samples and usage
Visual Studio .NET is the reference application for development tools related to .NET, but it's not the only .NET development tool. In fact, you might not need it at all. Visual Studio .NET is a little pricey at $1,900 per seat; MSDN Universal is $2,700. Multiply either of those price tags by 10 developers, and your budget may be strained.
Fortunately, Visual Studio .NET has some great support for third-party and legacy languages (FORTRAN, COBOL, Perl) and of course, for the current Microsoft languages: BASIC, C#, VB.NET, and C++. All you need is the command-line compiler for the specific Common Language Specification-compliant language.
In C++, developers use the #include directive when dealing with wrapper classes for COM objects, or the #import directive when importing types from C. In .NET’s Managed C++, the #using directive—in a form such as #using <mscorlib.dll>—is employed. This lets you import .NET types from any .NET assembly, regardless if that assembly was written in C, Perl, ADA, or another language.
When compiling a Managed C++ application, the developer can use the IDE’s design environment or the command line. To compile from the command line, make certain the /CLR switch is used to generate a .NET executable, like this: cl.exe MyFile.cpp /CLR /link /entry: main (assuming the entry point is named, main).
Compiling a .NET PE is similar to the process in Visual BASIC. NET, but it is geared more toward the way Visual Basic programmers work, with the names of classes instead of the file names of their libraries. The Imports keyword may be used in a class module, like so:
Public Module modSample
Console.WriteLine (“Hello World!”)
Next, it may be compiled from the command line as vbc.exe /t:exe /out:MyFile.exe MyFile.vb. That /t: option is handy because it specifies into which kind of program the project will be compiled.
Since all .NET PEs make extensive use of metadata, it's important to be able to access that data from an application. To do this, you'll use the .NET Framework’s System.Reflection capabilities. In C#, make certain the using System.Reflection directive is at the top of your code. Later in the code, you can retrieve the information about the methods exposed by the first type in the target PE's first module by using something like this:
Assembly asmblyMyFile = Assembly.LoadFrom(“MyFile.exe”);
Module modMyFile = assmblyMyFile.GetModules();
Type typsMyFile = modMyFile.GetTypes();
Type oType = typsMyFile;
MethodInfo MyMethods = oType.GetMethods();
At the very least, the methods returned include GetHashCode, Equals, ToString and GetType. These four methods are common to every .NET object.
In my previous article, I touched on JIT compilation and how IL isn’t compiled into machine code until it is needed. But, what else really happens when a .NET program runs? When the CLR executes a .NET PE, its Virtual Execution Engine takes the PE through three main phases: Class Loader, JIT Compilers, and Managed Native Code.
During the Class Loader phase, the OS Loader reads the metadata and code modules and identifies the “entry point” (the method that is executed when the code is first run). This incorporates a complex process of loading the class from the actual file on the hard drive or across the network, caching the Type information, performing some JIT and garbage collection housekeeping, and then passing control to the JIT Compilers
When a method is invoked and the class is loaded, the JIT compiler checks to make sure that the code is “type-safe,” Meaning that all variable types are exactly what they’re supposed to be. This is one reason why VB.NET does away with the Variant type. To be safely and efficiently managed, variable types must conform to the Common Type System types.
Managed native code
After the Class is verified, the JIT Compiler kicks in to turn the IL into managed native code. This happens only the first time a method is called. After that, the class loader (which tracks what code has been loaded or not) recognizes that the code is already loaded and compiled and doesn’t try to retrieve and recompile the same bit of code. The compiled code remains in memory until the process shuts down. At that point, the garbage collection functions of the code manager kick in and the memory that it was using is again made available to the system.
The code manager handles the class after it has been compiled from IL into Native Code. It performs such functions as garbage collection (returning unused resources to the operating system) and exception handling (a new, more efficient way to programmatically handle errors).
Ready to roll
Now you have a working knowledge of how the CLR works to allow programs written in any language to be packaged into assemblies. The resulting assemblies may run on any system supporting CLR. The CLS and the CTS, together with the IL programmers reference and MIL instruction set specification, define how the IL in an assembly can be generated, and the process it will go through to be rendered usable by the CLR.