Click here to Skip to main content
6,594,432 members and growing! (15,398 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate

Dynamic Code Generation and Code Compilation

By Wizard12

This is a simple application for generating the code dynamically and then compiling it dynamically.
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:2 Dec 2002
Views:78,597
Bookmarked:69 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
37 votes for this article.
Popularity: 6.20 Rating: 3.95 out of 5
4 votes, 10.8%
1

2
2 votes, 5.4%
3
7 votes, 18.9%
4
24 votes, 64.9%
5

Introduction

This is a simple application for generating the code dynamically and compiling it dynamically. I have given two classes, GenerateClass, which generates a class on the fly, and RunClass, which compiles the generated class.

For dynamic code generation and code compilation, we have the CodeDom technology. Before going into the sample, let me give you a short introduction to CodeDom. Some applications need the generation and or compilation of source code at run time. The .NET Framework SDK provides a standard mechanism called the Code Document Object Model (CodeDOM) that enables the output of the source code in multiple programming languages at run time, based on the single model that represents the code to render. The CodeDom namespace of .NET provides:

  1. A single model for rendering the source code. Therefore, we can extend the set of supported languages.
  2. A way to represent source code in a language independent object model.
  3. Programs can be dynamically created, compiled and executed at run time.

Details

CodeDom architecture can conceptually represent most programming constructs. However there are several limitations to the current CodeDom implementation. A few to mention are variable declaration list, unsafe modifier, aliasing the namespaces, nested namespaces, and others. To represent the constructs that are not provided by CodeDom, we can make use of the �Snippet� classes. To talk about the dynamic code generation and compilation, I have chosen the traditional example, �Hello World�. I tried to generate a code that will look like the following.

namespace Samples {
    using System;
    
    
    public class HelloWorld {
        
        public HelloWorld() {
            writeHelloWorld();
        }
        
        private void writeHelloWorld() {
            System.Console.WriteLine("Hello World!!");
        }
        
        public static void Main() {
            HelloWorld hw = new HelloWorld();
        }
    }
}

Now let us see how to generate the code. To generate the code, we need to create IcodeGenerator and CSharpCodeProvider. Have a TextWriter to write the generated class.

//output file name

string fileName = "HelloWorld.cs";

//text writer to write the code

TextWriter tw = new StreamWriter(new FileStream(fileName, FileMode.Create));

//code generator and code provider

ICodeGenerator codeGenerator = new CSharpCodeProvider().CreateGenerator();
			
CSharpCodeProvider cdp = new CSharpCodeProvider();
codeGenerator = cdp.CreateGenerator();

Declare the namespace for the class and adds the includes to the class.

CodeNamespace Samples = new CodeNamespace("Samples");
Samples.Imports.Add(new CodeNamespaceImport("System"));

Create the class.

CodeTypeDeclaration Class1 = new CodeTypeDeclaration("HelloWorld");
Samples.Types.Add(Class1);
Class1.IsClass = true;

The constructor for the created class is

CodeConstructor cc = new CodeConstructor();
cc.Attributes = MemberAttributes.Public;
cc.Statements.Add(mi1);
Class1.Members.Add(cc);

The method writeHelloWorld can be given as :-

CodeMemberMethod Method1 = new CodeMemberMethod();
Method1.Name = "writeHelloWorld";
Method1.ReturnType = new CodeTypeReference(typeof(void));
//Method1.Parameters.Add(

//  new CodeParameterDeclarationExpression(typeof(string), "text"));

CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression("System.Console"), 
    "WriteLine", new CodePrimitiveExpression("Hello World!!"));
Method1.Statements.Add(cs1);
Class1.Members.Add(Method1);

To create an executable, we should mention the entry point of the application. The method, Main is given as :-

CodeEntryPointMethod Start = new CodeEntryPointMethod();
Start.Statements.Add(
    new CodeSnippetStatement("HelloWorld hw = new HelloWorld();"));
Class1.Members.Add(Start);

Finally generate the source code file.

codeGenerator.GenerateCodeFromNamespace(Samples, tw, null);

Now, let us see how to compile and run the code at run time. For this we need the ICodeCompiler.

ICodeCompiler cc = new CSharpCodeProvider().CreateCompiler();

Set all the compiler parameters that you want. Note if you want to compile an assembly, set the GenerateExecutable to false.

CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll"); //includes

cp.GenerateExecutable = true; //generate executable

After setting the parameters, compile the code and get the results. Check out for errors, if any.

CompilerResults cr = cc.CompileAssemblyFromFile(cp, fileName);

if(cr.Errors.HasErrors)
{
    StringBuilder error = new StringBuilder();
    error.Append("Error Compiling Expression: ");
    foreach (CompilerError err in cr.Errors)
    {
        error.AppendFormat("{0}\n", err.ErrorText);
    }
    throw new Exception("Error Compiling Expression: " + error.ToString());
 } 

And the last step, get the assembly generated and create an instance to run.

Assembly a = cr.CompiledAssembly;
_Compiled = a.CreateInstance("Samples.HelloWorld");

That's all.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Wizard12


Member
Have a Master of Computer Applications degree from Anna University, Chennai, India. Working as a Technology Specialist for Cognizant, Bangalore, India.
Occupation: Web Developer
Location: India India

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralThanx for the example Pinmemberalphons12:16 24 Sep '06  
Questionsdk need PinmemberengMIK2:19 18 Jul '06  
Generalsigning Dynamic Code PinsussAnonymous7:50 14 Mar '05  
Generalmi1 Pinmembernetclectic5:09 3 Oct '03  
GeneralRe: mi1 Pinmemberwazupwiddat19:42 2 Feb '04  
GeneralCreating two generators PinmemberBo Norgaard0:50 5 Dec '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Dec 2002
Editor: Nishant Sivakumar
Copyright 2002 by Wizard12
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project