.Net: Assemblies (Part – 1)

Assemblies are the self-describing building blocks of .Net based applications. Assemblies contain Assembly manifest, which contains metadata information, contains a collection of data that describes the relationship between the elements in the assembly; Type metadata describing the exported types and methods; MSIL (Microsoft Intermediate Language) code that CLR (Common Language Runtime) executes; and a set of resources. All these parts can be in one assembly or spread across into several files.

Assemblies also contains references to other assemblies or modules. Assemblies contains one or more files and packaged in either .EXE files or in .DLL files.

There is a difference between normal DLL files and assembly DLL files. Normal DLL files doesn’t have manifest.

We can classify assemblies as either private assemblies or shared assemblies. Private assemblies are usually located in the application folders. Shared assemblies are commonly deployed into Global Assembly Cache (GAC). Shared assemblies are shared by several applications and have a dependency on it. Shared assemblies reduce the need for disk and memory space.

While creating shared assemblies, we must give the strong name to avoid naming or version conflicts with other assemblies. Strong names consists assembly name, version number, culture information and public key.

Lets create a simple assembly:

There are several ways to create assemblies. In this article we are using Visual C# & Visual Studio 2012 to create assemblies.

Let’s write a simple C# program, compile it and check how assemblies are created.

  • Just create a Hello class under CodeSteps namespace.
  • Add SayHello method to it. In SayHello method, display “Hello, World!” message on console.
  • Create the instance of Hello class from Main and call SayHello method.
  • Save the file as hello.cs.

Here is the C# code:

// hello.cs
//
using System;

namespace CodeSteps
{
    public class Hello
    {
        public void SayHello()
        {
            Console.WriteLine("Hello, World!");
        }

        public static void Main()
        {
            Hello hello = new Hello();

            hello.SayHello();
        }
    };
};

Now we have created a simple C# file hello.cs. To compile the file hello.cs using C# compiler, type the below command at command prompt.

csc hello.cs

C# compiler will compile the code and generate the .EXE file hello.exe. Once you run hello.exe file, it will display “Hello, World!” message on the screen.

Now lets check what hello.exe file contains. Type the below command to open hello.exe file using Intermediate Language disassembler tool (ildasm).

ildasm hello.exe

Microsoft’s IL DASM tool opens hello.exe and display the assembly information in its window.

Microsoft .Net - IL Disassembler - Assembly manifest

Microsoft .Net – IL Disassembler – Assembly manifest

Double click on MANIFEST text from the window. It will open assembly manifest information in another window.

Observe that hello.exe contains assembly manifest and the methods exported.

We will discuss more about assemblies in our upcoming articles.

1 comment for “.Net: Assemblies (Part – 1)

Leave a Reply