Resources » Code Snippets » Collections

Creating Dynamic Assembly: A Step Ahead Series

Updated: Category: Collections
Author: Member Level: GoldPoints: 10

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.

Did you like this resource? Share it with your friends and show your love!

Article by Gaurav Kumar Arora
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 Kumar Arora or read 157 articles authored by Gaurav Kumar Arora
Responses to "Creating Dynamic Assembly: A Step Ahead Series"

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


Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India