Under the covers of the .Net CLR

Daily Newsletters

Sign up to ZDNet UK's daily newsletter.

ANALYSIS
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. C programming
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 -- 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). BASIC programming
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:
Imports System
Public Module modSample
Sub Main()
Console.WriteLine ("Hello World!")
End Sub
End Module
 
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. System.Reflection
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[0].GetTypes();
Type oType = typsMyFile[0];
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. Runtime execution
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. Class Loader
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 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. Code manager
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.
Have your say instantly in the
Tech Update forum. Find out what's where in the new Tech Update with our
Guided Tour. Let the editors know what you think in the
Mailroom.

Post your comment

In order to post a comment you need to be registered and logged in.

You can also log in with Facebook. Log in or create your ZDNet UK account below

  • Login

Will not be displayed with your comment

By signing up for this service, you indicate that you agree to our Terms and Conditions and have read and understood our Privacy Policy. Questions about membership? Find the answers in the Community FAQ

Get ZDNet UK's daily newsletter

Enter your email address to sign up

ZDNet UK Live

GrueMaster

Nice review and very informative. One thing I'd like to add (in reply to whs001's 1st question), the main reason to have the same interface from...

19 minutes ago by GrueMaster on A tale of two distros: Ubuntu and Linux Mint
Frederick Wrigley

I'be been using Mint 12 since the RC came out, and I am far more happy with the Cinnamon, the Mate, and, yes (with extensions), theGnome 3...

1 hour ago by Frederick Wrigley via Facebook on A tale of two distros: Ubuntu and Linux Mint
bdantas

Excellent article. One small correction, though--although a fresh installation of Linux Mint 12 will, indeed, provide the user with a version of...

2 hours ago by bdantas on A tale of two distros: Ubuntu and Linux Mint
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

2 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

2 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Moley

For Gnome 2 die-hards, it is possible to add icons to the bottom panel (or top top panel, if you prefer) which provide the exact Gnome 2...

3 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
ramwellian

Your comments would seem pretty naive and immature. Your 'solution' appears to be, "gee, let's all just give in to the hackers and give them...

3 hours ago by ramwellian on Cloud computing security: no more oxymoron?
BugStalker

"Interesting thought ... If you installed Win7 as a dual boot on a machine that previously only had Linux, and it wrecked your Linux installation,...

4 hours ago by BugStalker on Windows 7 Declares War on GRUB
whs001

This is an excellent summary of Ubuntu and Mint and the interface differences between them. Most such articles take a very partisan position for...

4 hours ago by whs001 on A tale of two distros: Ubuntu and Linux Mint
Moley

@ewallace. Not so clear. Anyone can obtain the text, for example from here http://www.ustr.gov/webfm_send/2379. I support ACTA so long as it and...

4 hours ago by Moley on ACTA: Facts, misconceptions and questions
45283

I think WinRT is fantastic. I just wish it was an option for people that didn't want to go through Microsoft's App Store with its attendant...

7 hours ago by 45283 on Why Windows 8 needs architectural hygiene for WOA
Burn-IT

Nine people? £30m? Who's back pocket is that lot going in? And IF they say it is for new buildings, what about all the ones the government has...

8 hours ago by Burn-IT on Police set to launch three £30m e-crime hubs
ewallace

Just to be clear, nobody knows what is in the text of ACTA, here is a photograph of the text of ACTA http://twitpic.com/8h9iju as submitted to the...

8 hours ago by ewallace on ACTA: Facts, misconceptions and questions
fgvrg56

Unfortunately main issue is that ASUS is refusing to accept that they make some mistake on this version of asus Transformer prime. 1 - GPS sensor...

10 hours ago by fgvrg56 on Asus Eee Pad Transformer Prime Wi-Fi & GPS problems?
Ben Woods

@Marcus A fair question. Just talked with Archos which said it was working on an announcement for next week....

11 hours ago by Ben Woods on Archos confirms G9 Ice Cream Sandwich update schedule
Marcus Karlsson

Any update on this, considering the claimed "first week of February"?

12 hours ago by Marcus Karlsson via Facebook on Archos confirms G9 Ice Cream Sandwich update schedule
apexwm

Bill Goodrich : Just as al_langevin pointed out, with Windows Server 2008 there is no Services for Macintosh anymore. It's gone, not available....

20 hours ago by apexwm on Windows Server 2008 drops the ball for Mac compatibility
txtrainguy

Replying to an old topic that I'm currently facing with my CEO (who is on a Mac). Our servers are primarily Windows Servers, office is about...

1 day ago by txtrainguy on Windows Server 2008 drops the ball for Mac compatibility
k0tcs3

Sure, that makes perfect sense. Pay wrong-doers money and thank them for breaching your security and pointing out your flaws, that would surely...

1 day ago by k0tcs3 on US indicts Romanian over NASA climate change hack
Random_Error

I think he's referring specifically to Android apps, as Apple do regulate their App Store, but Google seem to let any old crap onto the Android store!

1 day ago by Random_Error on RIM: BlackBerry will keep 'garbage' apps out of store

Latest in Application Development