techmore.in

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

  1. Open Visual Studio.
  2. Select File > New > Project.
  3. Choose Class Library under the .NET category.
  4. 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

  1. Right-click on the project in Solution Explorer.
  2. Select Build.
  3. The DLL file will be generated in the bin/Debug or bin/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

  1. Open the project where you want to use the DLL.
  2. Right-click on the References or Dependencies folder in Solution Explorer.
  3. Select Add Reference or Add Project Reference.
  4. 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:

  1. Set breakpoints in the DLL project.
  2. Start debugging the main application project.
  3. 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

  1. Open a command prompt.

  2. Use the sn tool to generate a key pair:

    bash
    sn -k MyKey.snk

Sign the Assembly

  1. Open the AssemblyInfo.cs file in your DLL project.

  2. Add the following attribute:

    csharp
    [assembly: AssemblyKeyFile("MyKey.snk")]
  3. 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

  1. Open Developer Command Prompt for Visual Studio.

  2. Use the gacutil tool to install the DLL:

    bash
    gacutil -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.