C#-The Language : A Step Ahead Series-Part-II

C#-The Language : A Step Ahead Series-Part-II

This tutorial is a part of C#:2005-Beginners A Step Ahead Series book. Which tells about the assemblies:


This tutorial will deal with the great feature of assemblies from .NET. Before, this lets remind older days. Earlier you have component objects DLL to favor you for more flexibility of program and security. But, that was not right for every time and everywhere. Also with the same you have faced a problem called DLL Hell.

Delay Signing Concept

As it is pronounced from name that assembly is signed at the time of deploymentand not at execution time. There are policies of certain companies where die to security risks, the private key is not stand with all staff members. For delay signing global assembly attribute 'AssemblyDelaySigning' should be set to true.

There are following steps, favoring delay signing concept:

  1. Create a public / private key pair with string name utility [you have already done in previous example]

  2. Now, extract the public key [using switch -p with sn.exe] as:

  3. Developers, who have this public key file, will compile the assembly using /delaySign+ option. This way reserved the space for signature. While using Visual Studio2005, delay sign checkbox should be checked.

  4. As assembly does not have any signature so, turn off the verification of signatures using -vr switch with sn.exe.

  5. Finally, when assembly is ready to deploy then resign the assembly with actual key pair file [using sn.exe –R]

1. Signature verification should be turned off during the process of development.
2. Never distribute an assembly without signature verification

Implementing Cross Language Features

As you have already studied the topic in .NET Framework, now lets try to implement it here:
Here we have to compliant CLS rules

In the available example you have to create another .net language application lets say here you will create a Visual BAsic.NET class application [refer to Cross Language Implementation].

After adding reference as you have done in previous examples, compile and run the project.

Getting Assembly details at runtime

Revise our private Assembly example and add following method to the same and subsequent changes have to make in client application also as follows[marked with yellow]:

Native Image Generator [The ngen.exe]

Actually, Native Image Generator Compile the IL Code to native code at the time of installation. It makes the program to start faster as compilation at runtime no longer necessary. This utility installs the native image in the native image cache. The physical path of the directory is C:\WINDOWS\assembly\NativeImages<RuntimeVersion>.
After the image is created .Net runtime will use the image to run the code rather than rom the hard disk.

Features of ngen.exe

  • Native images load faster than MSIL because JIT compilation and type safety verification is eliminated.

  • If you are sharing code between process ngen.exe improves the performance significantly. As Native image generated Windows PE file so a single DLL file can be shared across applications. By contrast JIT produced code are private to an assembly and can not be shared.

  • Native images enable code between processes.

  • Native images require more storage space and more time to generate.

  • Assemblies in GAC do not benefit from Native image generator as the loader performs extra validation on the strong named assemblies thus hiding the benefits of Native Image Generator.

  • If any of assembly is changed then native image should also be updated.

  • You should have administrative privileges to run ngen.exe

Can assemblies be Static or dynamic?

Yes, assemblies can be static or dynamic. Static assemblies can include .NET framework type [interfaces and classes etc.], as well as resources for the assembly [bitmaps, jpeg files resources files and so on]. Static assemblies are stored on disk in PE files. You can also use the .Net framework to create dynamic assemblies, which run directly from memory and are not saved to disk before execution. You can save dynamic assemblies to disk after they have executed. Dynamic assembly can be created using the Reflector.Emit APIs.


Simply, the mechanism through which one can access metadata information is called reflection. Namespace System.Reflection can be used to browse through the metadata information. Using reflection you can also dynamically invoke methods using System.Type.Invokemember.

Take a look on following example:

/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : msdotnetheaven */
// File name : reflection.cs
using System;
using System.Reflection;
namespace CSharp.AStepAhead.reflection
class reflection
static void Main()

Type t = Type.GetType("System.Int32");
Console.WriteLine("\nType Name : {0}", t.Name);
Console.WriteLine("System Type :{0}", t.UnderlyingSystemType);
Console.WriteLine("Full Name : {0}", t.FullName);
Console.WriteLine("Base Type : {0}", t.BaseType);
Console.WriteLine("Is Class : {0}", t.IsClass);
Console.WriteLine("Assembly Info : {0}", t.Assembly);


In above example, by simply specifying the name of a type, you can query almost all aspects of the object. You can determine whether the object is a class, what its base system type is, and many other properties. Lets create another major example favoring reflection, create a class and access its metadata information using reflection:[refer to Using Reflection].

Creating a Dynamic Assembly

As mentioned earlier one can create a dynamic assembly using System.Reflection.Emit, lets try for the same [refer to Dynamic Assembly]


The .NET Framework includes a mechanism called the Code Document Object Model

[CodeDOM] that enables developers of programs that emit source code to generate source code in multiple programming languages at run time, based on a single model that represents the code to render.
To represent source code, CodeDOM elements are linked to each other to form a data structure known as a CodeDOM graph, which models the structure of some source code.

The System.CodeDom namespace defines types that can represent the logical structure of source code, independent of a specific programming language.

Simply, it is an object model which represents actually source code. It is designed to be language independent.

Some common uses for the CodeDOM include:

  • Templated code generation: generating code for ASP.NET, XML Web services client proxies, code wizards, designers, or other code-emitting mechanisms.

  • Dynamic compilation: supporting code compilation in single or multiple languages.

In Creating Code Dom application you can imagine the power of CodeDom. This example accepts the user inputs for class name, namespace, message to display and generate the .dll and .exe file(s) at specified path.


Hope, tutorial C#-The Language : A Step Ahead Series both Part-I & II are seem to valuable for you. Please do check the relevant codes.

Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 157 articles authored by Gaurav Aroraa


Author: Pradeep Iyer21 Mar 2009 Member Level: Gold   Points : 1

Hi Gaurav,

This is a great article.

You are helping the fresh developers a lot.

Keep on doing.

Pradeep Iyer

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: