This is a fully theoretical article. In this article, I am going to explain fundamentals of CLR. How CLR executes codes written in different languages. I am going to explain various components of MANAGED MODULES also.
Note: I have written this article on basis of my learning from the very nice book written by Jeffery Richter named CLR via C#. I suggest everyone to have a better understating on CLR read this book. Thanks to original author Jeffery. I just tried to make his words simpler for native reader like me.
Flow Chart of CLR execution model
CLR comes as part of .Net framework. This is a run time that is useable by different and varied programming language.
Features of CLR is as follows
- Memory Management
- Assembly Loading
- Exception Handling
- Thread Synchronization
Any language which targets its compiled output to CLR can avail above features.
CLR can work on any language; condition is compiler of that language targets the compiled output to the CLR. Microsoft has created much language which compilers targets to CRL.
This is either 32 bit or 64 bit Microsoft Standard Portable Executable File Format. These are the files required by CLR to execute.
Compiler of all high level language does the task of syntax checking and language analysis and targets the compiled output to CLR. All languages outputs PE 32 or PE32+ format files and that will get executed by CLR to produce CPU instructions.
The above diagram describes why .Net frameworks are so called Language independent. It is due to CLR. CLR is common for all languages.
Components of Managed Module
PE32 or PE32 + Header
- The standard of windows portable executable file format header is similar to common object filr format (COFF) headers.
- Header contains information about type of the file. That whether it is DLL, GIF etc.
- Header contains information about time stamp that when file built.
- If the managed module is having only IL then PE 32 (+) header is ignored by CLR.
- If the managed module contains CPU Instructions then PE 32 (+) header contains information about CPU instructions.
- This contains the information interpreted by CLR.
- This contains information about version of CLR needed to interpret the managed module.
- This contains Flags information.
- CLR header contains MethodOF token which says what is entry point (main method) of managed module.
Code generated by language compiler as compiled outputs. This IL code gets interpreted by CLR to produce CPU instructions.
Each managed modules contain Meta Data table. There are normally two types of Meta Data table.
Every compiler targeting the CLR must emit a full set of Meta data. Meta Data is set of tables that contain all the information about what defines in the module. This contains information about all the types and members used in the code. This also contains all the type and members referenced in the code. Meta Data is always associated with the files contain the IL. Meta Data packaged in the same dll or exe file.
Uses of Meta Data table
- Meta Data removes the natives' code like header files.
- Visual Studio uses Meta Data to perform work of IntelliSense. IntelliSense feature of visual studio uses Meta data tables to give suggestion while writing codes.
- CLR code verification uses Meta Data tables to make sure code only performs type safe operations.
- Meta Data performs work of serialization and deserialization.
- Garbage collector reads life time of object from Meta data table.
In this article, I discussed how high level language compiles source code to a managed module. And CLR reads this managed module to give CPU instructions as output. This was 1st part of series of article on CLR. Thanks for reading. I hope it was useful for you.