
In C#, an assembly is a fundamental unit of deployment, versioning, and security within the .NET framework. An assembly is a compiled code library used by applications...
In C#, an assembly is a fundamental unit of deployment, versioning, and security within the .NET framework. An assembly is a compiled code library used by applications to perform specific tasks, and it is typically packaged as a DLL (Dynamic Link Library) or EXE (Executable) file.
Assemblies are crucial because they contain the metadata, code, and resources needed by .NET applications. Understanding assemblies is essential for managing code reuse, deploying applications, and handling versioning and security in .NET.
An assembly is a collection of one or more files (DLLs or EXEs) that are compiled from C# code and other resources. It is the basic building block of .NET applications. Assemblies contain:
Assemblies can either be private (used by a single application) or shared (used by multiple applications).
Assemblies in .NET can be categorized into two types:
An assembly is a collection of files, and it contains several key components:
A strongly named assembly is an assembly that has a unique identity, which is guaranteed by signing the assembly with a public and private key pair. A strongly named assembly includes the following elements:
A strongly named assembly can be stored in the Global Assembly Cache (GAC), which allows it to be shared across multiple applications. When creating a strongly named assembly, you are essentially ensuring that the assembly has a unique identity, which helps avoid conflicts when different versions of the same assembly are used.
To create a strongly named assembly, you need to generate a public/private key pair and sign the assembly.
The Global Assembly Cache (GAC) is a central repository for storing shared assemblies in .NET. Assemblies that are shared by multiple applications are typically stored in the GAC.
You can install assemblies into the GAC using tools like the GacUtil tool or by adding them through Visual Studio.
gacutil command to install the assembly into the GAC:gacutil -i <path-to-assembly>\MyAssembly.dll
This command will install the specified assembly into the GAC.
You can add references to assemblies in C# projects through the .NET CLI, Visual Studio, or by editing the project file manually.
To add a reference to an assembly:
dotnet add reference <path-to-assembly>\MyAssembly.dll
For a NuGet package, you can use the CLI to install it:
dotnet add package Newtonsoft.Json
One of the key features of assemblies is versioning. Assembly versioning allows the runtime to ensure compatibility when different versions of assemblies are used. There are two types of version numbers in C#:
This is a version number (typically in the format major.minor.build.revision) that helps identify the assembly. The version number is specified when the assembly is compiled and is included in the assembly’s metadata.
[assembly: AssemblyVersion("1.0.0.0")]
The file version is another version number that is used to describe the file itself (separate from the assembly version). It is typically set to match the assembly version but can be different.
[assembly: AssemblyFileVersion("1.0.0.0")]
You can inspect the metadata of assemblies at runtime using reflection in C#:
using System;
using System.Reflection;
class Program
{
static void Main()
{
// Load an assembly
Assembly assembly = Assembly.LoadFrom("MyClassLibrary.dll");
// Get assembly information
Console.WriteLine($"Assembly Full Name: {assembly.FullName}");
Console.WriteLine($"Assembly Location: {assembly.Location}");
// Get types from the assembly
Type[] types = assembly.GetTypes();
foreach (var type in types)
{
Console.WriteLine(type.Name);
}
}
}
This code loads an assembly, displays its full name and location, and lists all the types defined in the assembly.
An assembly in C# is a compiled code library that contains executable code, metadata, resources, and a manifest. Assemblies are the building blocks of .NET applications and are essential for managing code reuse, versioning, and security.
Understanding assemblies is crucial for deploying, managing, and maintaining .NET applications, as they provide the structure, versioning, and reusability needed in modern software development.
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.