An assembly is the fundamental unit of code that the .NET Framework uses for application management, distribution, and versioning.
Assemblies allow encapsulation of specific functionalities that can be reused across different projects. Furthermore, they can be distributed as independent files, simplifying the sharing of components or applications.
Finally, the manifest of the assemblies allows management of different versions of the same assembly (this is very important for the proper functioning of .NET applications).
Assemblies are the basic building block with which applications in .NET are constructed. Let’s say that if .NET were to build a building, the assemblies would be the bricks used to construct the walls.
Creation and Types of Assembly
To create an assembly, the source code must be compiled. This process transforms the C# source code into a binary file that can be:
- Executable Assemblies (EXE): These contain an entry point, like the
Main
method, and can be executed directly by the operating system. - Library Assemblies (DLL - Dynamic Link Library): These assemblies contain code that can be reused by other applications. They cannot be executed on their own.
The final assembly file includes compiled code, metadata, and additional resources such as images or configuration files.
Structure of an Assembly
An assembly in C# is composed of several key elements:
IL Code (Intermediate Language)
The C# source code is compiled into an intermediate language called IL, which is executed by the Common Language Runtime (CLR). This IL is platform-independent, allowing the same assembly to be executed in different .NET environments.
Manifest
Contains information about the assembly itself, such as its version, dependencies, and security permissions. It acts as the “table of contents” for the assembly.
- Assembly name.
- Version.
- Culture.
- List of files that make up the assembly.
- Dependencies on other assemblies.
- Security permissions.
Metadata
The metadata contains detailed descriptions of the types, methods, properties, and events defined in the assembly. It facilitates reflection and interoperability between languages, allowing other assemblies or applications to understand and effectively utilize the code.
Resources
Assemblies can include resources such as images, sound files, localized strings, and other files necessary for the application. These resources are embedded directly in the assembly, simplifying its distribution and use.