Resources » Code Snippets » Collections

Creating Dynamic Assembly: A Step Ahead Series


Posted Date: 01-Nov-2008  Last Updated:   Category: Collections    
Author: Member Level: Gold    Points: 10



Creating Dynamic Assembly: A Step Ahead Series


Introduction:


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.");
msilG.Emit(OpCodes.Ret);

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

public Type T
{
get
{
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!

Responses to "Creating Dynamic Assembly: A Step Ahead Series"

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

Feedbacks      

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

    Aman Gandhi
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India