C# Assembly - How to create and share assembly

    1 Votes

An assembly is a single deployable unit which contains all the information about the implementation of classes, structure and interfaces. The executable code is stored in Assemblies. The assemblies are the self describing unit and the programs in .Net are constructed through these assemblies.

The .exe and .dll files are the kinds of assembly. They store all information about itself and it is known as assembly metadata which includes name and the version number of the assembly. Namespaces are also stored in assemblies. Assemblies can be private or shared where private assembly is accessible by single application whereas shared assembly is shared by multiple application. Assembly are classified into two:

  • Static Assembly: It is created when we compile our code using compiler. In this it is stored on the hard disk in the form of portable executable file.
  • Dynamic Assembly: It is created during run time and it occupies memory only.

Assembly can be stored in single and or multiple files. In single file the MSIL code, metadata, resources and assembly metadata are all stored in single file. In multiple file assembly they are separated in different file. Assembly contains the four elements:

  • Assembly Manifest – It contains the assembly metadata which describes the assembly and its contents.
  • Source Code – It is compiled into Microsoft Intermediate Language (MSIL).
  • Type Metadata – It defines all types, properties and methods.
  • Resources – Icons, images, text strings and other resources.

Assembly Manifest

Assembly whether static or dynamic it contains a collection of data which describes how the elements in the assembly are related to each other. The assembly manifest contains the assembly metadata. An assembly manifest contains the metadata which are required to specify the assembly version requirement and security identity, and the metadata are required to define the scope of the assembly and resolve the references to resources and classes.

It contains the assembly name, version number, culture, and strong name information that makes up the assembly's identity. It also contains list of all the files in the assembly, type reference information and list of other assemblies that are referenced by the assembly. The assembly manifest is stored in either a Portable Executable file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a separate Portable Executable file that contains only assembly manifest information.

Functions of Assembly Manifest:

  • Manifest lists the files that make up the assembly.
  • It lists other assemblies on which the assembly depends.
  • It makes the assembly self-describing.

How to create assembly?

We can create single file or multiple file assembly using IDE like Visual Studio 2005 or compiler and tools provided by Windows Software Development Kit (SDK). The single file is a simple assembly and is loaded in a single application and it cannot be referenced with other assemblies.

The multiple file assembly can be created with several codes and resource files. It can be shared by multiple applications but the shared assembly should have a strong name and can be deployed in Global Assembly Cache. For example:

public class Numbers
{   private int total;
     public Numbers ( )
       {     total = 0;   //initializing total
   public void AddNumber (int num)
    {    total += num;
  public int GetTotal ( )
   {    return total;

The Numbers class maintains the running total of integer values using AddNumber method. The total is obtained using GetTotal method. The second example is

public class SumArray
{   public static int SumArrayOfIntegers (int[ ] arr)
        {       Numbers x_num = new Numbers ( );
                 foreach (int a in arr)
               {     x_num.AddNumber (a);
               return x_num.GetTotal ( );

Now save the file as Numbers.cs and SumArray.cs

Creating Single File Assembly

In C# the single file assembly is the default assembly type created. The assembly metadata and MSIL statements are included in a single file. The statement to execute the single file assembly:

 csc /out: SingleFileAssembly.dll /target: library Numbers.cs SumArray.cs

This statement create a single file assembly which is named as SingleFileAssembly.dll, which contains the compiled Numbers and SumArray types, the /out argument allows to specify the name of the created file, /target option allows to choose between application and library assemblies.

Creating a Multifile Assembly

Creating a multifile assembly is complicated than creating a single file assembly but it also provides some additional flexibility. The statement to execute Numbers type into its own module:

 csc /out: Numbers.netmodule /target: module Numbers.cs

This command will create a new module which is called Numbers.netmodule. The netmodule is the standard for module files same as dll for library files. Now the implementation of SumArray type into a module will be

 csc /out: SumArray.netmodule /target: module SumArray.cs

Now we can create a multifile assembly that contains modules by using the Assembly Linker tool (al.exe). The statement which creates a library assembly is called as MultiFileAssembly.dll:

 Al /out: MultiFileAssembly.dll /target: library Numbers.netmodule SumArray.netmodule

How to share assembly?

Assemblies can be private or shared by default. To share an assembly with other applications it should be placed in Global Assembly Cache (GAC).

In each computer common language runtime is installed where a machine wide code cache is known as Global Assembly Cache. It stores the assembly which are designated to be shared by several applications on the computer.

Steps for sharing assembly:

1. Create your class file. Like

using System;
namespace SharedAssembly
 public class Fruit
 {    public void start ()
              Console.WriteLine ("Mango Apple");

2. Assign a strong name

It is a string which is a large collection of alphabet and numbers. It is a big string so no one can access it without permission. Open the command prompt of Visual Studio and type

 D:\shared assembly> sn-k shrd.snk

It will create a key name shrd.snk

3. Now apply this key on your class file by writing the code in .dll in source file

using System.Reflection;
[assembly: AssemblyKeyFile("shrd.snk")]
Class file 

using System;
using System.Reflection;
[assembly: AssemblyKeyFile("shrd.snk")]
namespace SharedAssembly
    public class Fruit
        public void start()
            Console.WriteLine("Mango Apple");

4. Now compile the code file and create a .dll file of Fruit class.

5. Now register or install .dll into GAC. Like this

 D:\shared assembly>gacutil /i sharedAss.dll

6. Now the application is as follows:

using System;
using SharedAssembly;
public class Program
    public static void Main(string [ ]args)
     Fruit f=new Fruit();
     Console.Read ( );

7. Now compiled the program.

 D: \> csc /r: d: \shared assembly\sharedAss.dll Program.cs

8. Now run the program by using this command

 D: \>Program
Planning to do an MBA?
A quick all-in-one MBA entry manual for MBA Aspirants. Book covers
  • Possible MBA Tests & Exam Preparation
  • Tips to choose right MBA Program
  • Essay, Resume & Letter of Recommendation
  • MBA Interview Preparation
  • MBA Financial Planning
Price - 6.99$

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

C# Overview & Evolution of C# Language
Introduction of .NET Framework
C# Datatypes - Datatype Conversion, Boxing & Unboxing
C# Metadata, Metadata Tables and Benefits
C# - Common Language Intermediate (CIL)
C# - Class and Objects
C# Garbage Collection
C# Indexers
C# Exception Handling - User-Defied Exceptions
C# Inheritance - Single, Hierarchical, Multilevel & Multiple Inheritance
C# Polymorphism - Static and Dynamic polymorphism
C# Abstract Classes
C# Structures
C# Delegates and Events
C# Collections
C# XML Read and Write
C# Windows Presentation Foundation (WPF)
Language Integrated Query (LINQ)