C Sharp

Building Assemblies

If you create a DLL with the /t:library switch, you won't be able to add it to another assembly. This is because the compiler automatically generated a manifest for the DLL, and therefore the DLL itself is an assembly. To see this in action, look at the following example. We have a DLL (Module1Server.cs) that has a dummy type called Module1Server.

// Module1Server.cs
// build with the following command line switches
//     csc /t:library Module1Server.cs
public class Module1Server
{
}

This DLL is then referenced by the client code (Module1Client.cs): -

// Module1ClientApp.cs
// build with the following command line switches
//     csc Module1ClientApp.cs /r:Module1Server.dll
using System;
using System.Diagnostics;
using System.Reflection;
class Module1ClientApp
{
    public static void Main()
    {
        Assembly DLLAssembly = Assembly.GetAssembly(typeof(Module1Server));
        Console.WriteLine("Module1Server.dll Assembly Information");
        Console.WriteLine("\t" + DLLAssembly);
        Process p = Process.GetCurrentProcess();
        string AssemblyName = p.ProcessName + ".exe";
        Assembly ThisAssembly = Assembly.LoadFrom(AssemblyName);
        Console.WriteLine("Module1Client.exe Assembly Information");
        Console.WriteLine("\t" + ThisAssembly);
    }
}

Now let's say you built these two modules by using these switches: -

csc /t:library Module1Server.cs
csc Module1ClientApp.cs /r:Module1Server.dll

Running the code at this point results in the following output and proves that both the EXE and the DLL exist in their own distinct assemblies: -

Module1Server.dll Assembly Information
    Module1Server, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Module1Client.dll Assembly Information
    Module1Client, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

In fact, if you were to change the access modifier of the Module1Server class from public to internal, the client code wouldn't compile because by definition the internal access modifier specifies that the type being modified is accessible only to other code in the same assembly.

by BrainBellupdated
Advertisement: