.NET Assemblies
Assemblies are the fundamental building blocks of .NET applications. They are the unit of deployment, versioning, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality.
What is an Assembly?
An assembly is a compiled code library or executable. It contains:
- Type Metadata: Definitions of all the types (classes, structs, interfaces, enums, delegates) exposed by the assembly.
- Intermediate Language (IL) Code: The compiled code for each method, represented in Microsoft Intermediate Language (MSIL). This code is later Just-In-Time (JIT) compiled to native machine code by the .NET runtime.
- Symbol Information: Optional debugging information.
- Assembly Manifest: A table of contents that describes the assembly itself. It contains metadata about the assembly, including its version, culture, name, public key token, and the types it contains. It also lists other assemblies that this assembly depends on.
Assemblies are typically distributed as either Dynamic Link Libraries (.dll
) or executable files (.exe
).
Assembly Manifest
The assembly manifest is crucial for the Common Language Runtime (CLR) to understand and manage the assembly. It provides essential information such as:
- The assembly's identity (name, version, culture, strong name).
- The types defined within the assembly.
- The files that make up the assembly (if it's a multi-file assembly).
- The assembly's dependencies on other assemblies.
- The set of permissions required by the assembly.
The manifest can be embedded within a single assembly file or stored in a separate file.
Types of Assemblies
Assemblies can be categorized based on their content and purpose:
- Executable Assemblies: These are the main entry points for applications (
.exe
files). They contain aMain
method. - Library Assemblies: These are reusable code libraries (
.dll
files) that contain types and methods that can be called by other assemblies. - Satellite Assemblies: These assemblies contain localized resources for an application, such as strings and images for different cultures.
Strongly Named Assemblies
A strongly named assembly is one that has a unique identity based on its name, version, culture, and a digital signature generated from a public-private key pair. This helps prevent assembly conflicts and provides better versioning control.
Strongly named assemblies are essential for creating shared assemblies that can be deployed in the Global Assembly Cache (GAC).
The Global Assembly Cache (GAC)
The GAC is a machine-wide code cache maintained by the CLR. Assemblies that are intended to be shared among multiple applications can be installed into the GAC. This allows applications to reference and use these shared assemblies without needing them to be in the application's own directory.
Only strongly named assemblies can be installed in the GAC.
Example: Referencing an Assembly
When you create a .NET project, you often add references to other assemblies (like System.dll
, System.Core.dll
, or custom libraries). This tells the compiler and the CLR where to find the types your code uses.
// In a C# project, adding a reference to System.Xml.dll
// would allow you to use types like XmlDocument:
using System;
using System.Xml; // This 'using' directive implies a reference to System.Xml.dll
public class DocumentProcessor
{
public void LoadDocument(string filePath)
{
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
Console.WriteLine("Document loaded successfully.");
}
}
Key Concepts
- Assembly Binding: The process by which the CLR locates and loads an assembly into an application domain.
- Assembly Versioning: How the CLR manages different versions of the same assembly.
- Assembly Loading: The runtime behavior of loading assemblies into memory.