Understanding .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. 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:
using System;
public class Demo
{
public void SayHello()
{
Console.WriteLine("Hello from Demo class");
}
}
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.