C# - DLLs
In C#, a DLL (Dynamic Link Library) is a file that contains compiled code, which can be used by other applications or libraries. DLLs are a fundamental part of .NET development, allowing you to modularize code, reuse components, and manage dependencies efficiently. Here’s a detailed overview of working with DLLs in C#:
1. Creating a DLL
To create a DLL in C#, you follow these steps:
1.1. Create a Class Library Project
- Open Visual Studio.
- Select
File > New > Project
. - Choose
Class Library
under the.NET
category. - Name your project and solution, then click
Create
.
1.2. Write Code for the DLL
In the class library project, you can write classes, methods, and other types that you want to expose through the DLL.
csharp// MyLibrary.cs
using System;
namespace MyLibrary
{
public class Greeter
{
public string Greet(string name)
{
return $"Hello, {name}!";
}
}
}
1.3. Build the DLL
- Right-click on the project in Solution Explorer.
- Select
Build
. - The DLL file will be generated in the
bin/Debug
orbin/Release
folder, depending on your build configuration.
2. Using a DLL
To use a DLL in another project, follow these steps:
2.1. Add Reference to the DLL
- Open the project where you want to use the DLL.
- Right-click on the
References
orDependencies
folder in Solution Explorer. - Select
Add Reference
orAdd Project Reference
. - Browse to the location of your DLL file and select it.
2.2. Use the DLL in Your Code
Once the reference is added, you can use the classes and methods provided by the DLL.
csharp// Program.cs
using MyLibrary; // Import the namespace of the DLL
class Program
{
static void Main()
{
Greeter greeter = new Greeter();
string message = greeter.Greet("World");
Console.WriteLine(message); // Outputs: Hello, World!
}
}
3. Types of DLLs
- Class Library DLLs: Contain reusable code and components (e.g., business logic, utility functions).
- COM DLLs: Used to expose .NET classes to COM clients (e.g., older applications or languages).
4. Debugging DLLs
To debug a DLL:
- Set breakpoints in the DLL project.
- Start debugging the main application project.
- When the application uses the DLL, the debugger will hit the breakpoints in the DLL code.
5. Versioning and Assembly Information
DLLs contain assembly metadata that includes version information, culture, and strong naming. You can modify these settings in the AssemblyInfo.cs file:
csharp// AssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("MyLibrary")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MyLibrary")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
6. Strong Naming DLLs
Strong naming provides a way to uniquely identify assemblies and ensure their integrity. It involves signing the DLL with a public/private key pair.
Generate a Key Pair
Open a command prompt.
Use the
sn
tool to generate a key pair:bashsn -k MyKey.snk
Sign the Assembly
Open the
AssemblyInfo.cs
file in your DLL project.Add the following attribute:
csharp[assembly: AssemblyKeyFile("MyKey.snk")]
Rebuild the project to sign the DLL.
7. Deploying DLLs
When deploying applications that use DLLs, ensure that the DLLs are included in the application directory or the Global Assembly Cache (GAC) if they are shared among multiple applications.
8. Global Assembly Cache (GAC)
The GAC is used to store shared assemblies that need to be accessible to multiple applications on the same machine.
Install a DLL into the GAC
Open Developer Command Prompt for Visual Studio.
Use the
gacutil
tool to install the DLL:bashgacutil -i MyLibrary.dll
Summary
DLLs in C# are essential for creating modular, reusable, and maintainable code. They allow you to separate concerns, manage dependencies, and encapsulate functionality. By creating, using, and managing DLLs effectively, you can enhance the structure and organization of your applications. Understanding concepts like assembly information, strong naming, and GAC further supports robust and scalable application development.