What Is an Assembly?
An assembly is the fundamental unit of deployment, versioning, and reuse in the .NET Framework. It is a compiled code library used for deployment, consisting of one or more files (DLLs, EXEs, and related resources).
Structure of an Assembly
Every assembly contains the following components:
Component | Description |
---|---|
Manifest | Metadata about the assembly (identity, version, culture, etc.) |
Types | Compiled .NET types (classes, structs, interfaces) |
Resources | Embedded files such as images, strings, or data files |
Modules | One or more files that make up the assembly |
Assembly Manifest
The manifest contains essential information that the CLR uses to locate, load, and validate an assembly.
<assembly
name="MyLibrary"
version="1.2.0.0"
culture="neutral"
publicKeyToken="b77a5c561934e089">
<file name="MyLibrary.dll"/>
<dependency>
<assemblyIdentity name="System.Core" version="4.0.0.0" publicKeyToken="b77a5c561934e089"/>
</dependency>
</assembly>
Strong-Named Assemblies
Strong naming provides a unique identity to an assembly by using a public/private key pair.
- Ensures version uniqueness.
- Enables side‑by‑side execution.
- Provides tamper‑proofing.
Loading Assemblies
Assemblies can be loaded using several APIs:
// Load by display name (from GAC or probing path)
Assembly asm = Assembly.Load("MyLibrary, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
// Load from a specific file path
Assembly asm2 = Assembly.LoadFrom(@"C:\libs\MyLibrary.dll");
// Load into a custom context
var context = new AssemblyLoadContext("MyContext", isCollectible: true);
Assembly asm3 = context.LoadFromAssemblyPath(@"C:\libs\MyLibrary.dll");
Best Practices
- Prefer strong‑named assemblies for shared libraries.
- Keep assembly size small—use satellite assemblies for resources.
- Version assemblies carefully; follow semantic versioning.
- Avoid loading assemblies from untrusted sources.