CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

.Net – Assemblies – An Introduction

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 contain references to other assemblies or modules. Assemblies contain one or more files and are packaged in either .EXE files or in .DLL files.

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

Classification of Assemblies

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 them. Shared assemblies reduce the need for disk and memory space.

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

Create and compile 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 the CodeSteps namespace.
  • Add SayHello method to it. In the SayHello method, display the “Hello, World!” message on the console.
  • Create the instance of Hello class from Main and call the 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 a command prompt.

csc hello.cs

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

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

ildasm hello.exe

Microsoft’s IL DASM tool opens hello.exe and displays 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 the assembly manifest and the methods exported.

We will discuss more assemblies in our upcoming articles.

.Net – Assemblies – An Introduction

One thought on “.Net – Assemblies – An Introduction

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
Exit mobile version