The System.CodeDom namespace contains classes that can be used to represent the elements and structure of a source code document. The classes in this namespace can be used to model the structure of a source code document that can be output as source code in a supported language using the functionality provided by the System.CodeDom.Compiler namespace.

You just wonder why somebody wants to generate assemblies dynamically at runtime and execute. Take ASP.NET for example, it uses the CodeDOM to create object graphs that it compiles into assemblies that can render HTML pages and controls.

using System;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;

namespace Demo.CodeDOM
class ClsMyDOM
static void Main(string[] args)
StreamWriter sw = File.CreateText(“RunTimeGen.cs”);
sw.WriteLine(“using System;”);
sw.WriteLine(“class ClsDynamicGen{“);
sw.WriteLine(“public static void Main(){“);
sw.WriteLine(@”System.Console.WriteLine(“”This code is generated Dynamically””);”);

CSharpCodeProvider csCompiler = new CSharpCodeProvider();
ICodeCompiler iCodeCompiler= csCompiler.CreateCompiler();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.OutputAssembly = “RuntimeGen.exe”;
compilerParams.GenerateExecutable = true;

CompilerResults compilerResults= iCodeCompiler.CompileAssemblyFromFile(compilerParams,”RunTimeGen.cs”);
public static void DisplayCompilerResults(System.CodeDom.Compiler.CompilerResults cr)
// If errors occurred during compilation, output the compiler output and errors.
if( cr.Errors.Count > 0 )
for( int i=0; i<cr.Output.Count; i++ )
Console.WriteLine( cr.Output[i] );
for( int i=0; i<cr.Errors.Count; i++ )
Console.WriteLine( i.ToString() + “: ” + cr.Errors[i].ToString() );

// Display information about the compiler’s exit code and the generated assembly.
Console.WriteLine( “Compiler returned with result code: ” + cr.NativeCompilerReturnValue.ToString() );
Console.WriteLine( “Generated assembly name: ” + cr.CompiledAssembly.FullName );
if( cr.PathToAssembly == null )
Console.WriteLine( “The assembly has been generated in memory.” );
Console.WriteLine( “Path to assembly: ” + cr.PathToAssembly );

// Display temporary files information.
if( !cr.TempFiles.KeepFiles )
Console.WriteLine( “Temporary build files were deleted.” );
Console.WriteLine( “Temporary build files were not deleted.” );
// Display a list of the temporary build files
IEnumerator enu = cr.TempFiles.GetEnumerator();
for( int i=0; enu.MoveNext(); i++ )
Console.WriteLine( “TempFile ” + i.ToString() + “: ” + (string)enu.Current );



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s