Skip Navigation LinksHome > Categories > Code from a Category

Understanding .NET Class Loader



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 3031
Add Date: 10/12/2012
In this Article we are going to look at Class Loader which is one of the components available in .Net Framework Common Language Runtime - CLR.
Class Loader is responsible for loading the class after checking the metadata. It loads the class for execution only if the resources are available on the client machine and if the Version and the Security Token of the dll is same as in the metadata.

Learn what is .NET Class Loader

In this Article we are going to look at Class Loader which is one of the components available in .Net Framework - Common Language Runtime (CLR).

Suppose you have a Demo class as shown below:

We develop this code using Visual Studio IDE. The computer on which we develop this code must have .NET framework installed on it. The development environment has .NET framework which has .NET API, CLR and other tools required to develop this code.

Above code is written in C# language so when you build the application. .NET framework uses C# compiler(csc.exe) to compile the code. When the C# compiler compiles the code it does the following:

1. It checks if the System namespace is available in the class. That is it checks if we have imported the System namespace in the class by using this statement: "using System; "
2. Then it checks if the Console Class is available in the System namespace.
3. Then it checks if the WriteLine method is available in the Console Class.
4. Since the System namespace is available in the MSCORLIB.dll file. Compiler looks into the Local library (MSCORLIB.dll) and using the Dll, DLL Version and the security token of this dll file it generates Metadata.
5. It converts the Source Code into MSIL code.
6. Now it generates a Demo.exe file which contains both the MSIL code and the Metadata.
7.This Demo.exe file is given to client.


Let us see what happens on the client side:

1. Client must have the .NET Framework installed on the machine.
2. When the Demo.exe file is executed by the client. It loads the Demo class.
3. While it loads the Console class which is present in the API located in .NET framework, It checks if the MSCORLIB dll is present in the .NET framework.
4. If the dll is present it cross checks if the information present in the metadata is same as MSCORLIB dll. That means it cross checks if the MSCORLIB dll is present in API. If Yes then if the version and Security Token of the dll is same as in the metadata file.
5. Console class is loaded only if DLL, its version and its security Token are matching with that of metadata file information otherwise it gives "Resource not found" error.

All the above checks are done by ClassLoader which is one of the tools of CLR.

ClassLoader always checks Metadata. It performs: Resource checking, Version checking and Security checking.

On the Client machine conversion of MSIL code to Native code by JIT(Just-In Time) compiler happens only if Class Loader loads the class.

So the .NET library is utilized at compile time and execution time.
Thus Class Loader is responsible for loading the class after checking the metadata. It loads the class for execution only if the resources are available on the client machine and if the Version and the Security Token of the dll is same as in the metadata.

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.