Creating Dynamic Assembly: A Step Ahead Series

Creating Dynamic Assembly: A Step Ahead Series


This is a continuation to my earlier post C#-The Language : A Step Ahead Series-Part-II
The following code snippet showed how to create a dynamic assembly:

/* 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 : dynamicAssembly.cs
//This demostrates how to build a dynamic assembly

using System;
using System.Reflection;
using System.Reflection.Emit;
namespace CSharp.AStepAhead.dynamicAssembly
class CodeGenerator
Type t;
AppDomain currentDomain;
AssemblyName assemName;
AssemblyBuilder assemBuilder;
ModuleBuilder moduleBuilder;
TypeBuilder typeBuilder;
MethodBuilder methodBuilder;
ILGenerator msilG;

public static void Main()
CodeGenerator codeGen = new CodeGenerator();
Type myType = codeGen.T;
if (myType != null)
object o = Activator.CreateInstance(myType);
MethodInfo helloWorld = myType.GetMethod("HelloWorld");
if (helloWorld != null)
// Run the HelloWorld Method
helloWorld.Invoke(o, null);
else //if method info null
Console.WriteLine("Could not retrieve MethodInfo");
else //if type null
Console.WriteLine("Could not access the Type");

public CodeGenerator()
// Get the current Application Domain.
// This is needed when building code.
currentDomain = AppDomain.CurrentDomain;

// Create a new Assembly for Methods
assemName = new AssemblyName();
assemName.Name = "dynamicAssembly";
assemBuilder = currentDomain.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run);

// Create a new module within this assembly
moduleBuilder = assemBuilder.DefineDynamicModule("dynamicAssemblyModule");

// Create a new type within the module
typeBuilder = moduleBuilder.DefineType("dynamicAssemblyClass", TypeAttributes.Public);

// Now add the HelloWorld method to the class that was just created.
methodBuilder = typeBuilder.DefineMethod("HelloWorld", MethodAttributes.Public, null, null);

// Now generate some Microsoft Intermediate Language Code that simply
// writes a line of text out to the console.
msilG = methodBuilder.GetILGenerator();
msilG.EmitWriteLine("Hello! This is a dynamic assembly.");

// Create a type.
t = typeBuilder.CreateType();

public Type T
return this.t;

Anyone can create a dynamic assembly using System.Reflection.Emit.

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


No responses found. Be the first to comment...

  • 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: