Dynamically creating C# and VB code from CodeDOM

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

namespace Demo.CodeDOM
{

public class DynamicDOM
{
public DynamicDOM(){}
static void Main(string[] args)
{
CodeCompileUnit compileUnit = new CodeCompileUnit();

CodeNamespace samples = new CodeNamespace(“Samples”);
samples.Imports.Add( new CodeNamespaceImport(“System”) );

compileUnit.Namespaces.Add( samples );
CodeTypeDeclaration class1 = new CodeTypeDeclaration(“MyClass”);
samples.Types.Add(class1);

CodeEntryPointMethod start = new CodeEntryPointMethod();
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression(“System.Console”),
“WriteLine”, new CodePrimitiveExpression(“Hello World! This code is dymnamically created and compiled”) );
start.Statements.Add(cs1);

CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(“System.Console”),
“ReadLine”);
start.Statements.Add(cs2);
class1.Members.Add( start );

Console.WriteLine(“Cs File Generated —– {0}”,GenerateCSharpCode(compileUnit));
Console.WriteLine(CompileCSharpCode(“HelloWorldcs.cs”,”HelloWorldcs.exe” ));

Console.WriteLine(“VB.Net File Generated —– {0}”,GenerateVBCode(compileUnit));
Console.WriteLine(CompileVBCode(“HelloWorldvb.vb”,”HelloWorldvb.exe” ));
Console.ReadLine();

}
public static String GenerateCSharpCode(CodeCompileUnit compileunit)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeGenerator gen = provider.CreateGenerator();
String sourceFile;
if (provider.FileExtension[0] == ‘.’)
{
sourceFile = “HelloWorldcs” + provider.FileExtension;
}
else
{
sourceFile = “HelloWorldcs.” + provider.FileExtension;
}
IndentedTextWriter tw = new IndentedTextWriter( new StreamWriter(sourceFile, false), ” “);
gen.GenerateCodeFromCompileUnit(compileunit, tw,new CodeGeneratorOptions());
tw.Close();
return sourceFile;
}
public static bool CompileCSharpCode(String sourceFile, String exeFile)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add( “System.dll” );

cp.GenerateExecutable = true;
cp.OutputAssembly = exeFile;
cp.GenerateInMemory = false;
CompilerResults cr = compiler.CompileAssemblyFromFile(cp, sourceFile);
if(cr.Errors.Count > 0)
{
Console.WriteLine(“Errors building {0} into {1}”,
sourceFile, cr.PathToAssembly);
foreach(CompilerError ce in cr.Errors)
{
Console.WriteLine(” {0}”, ce.ToString());
Console.WriteLine();
}
}
else
{
Console.WriteLine(“Source {0} built into {1} successfully.”,
sourceFile, cr.PathToAssembly);
}
if (cr.Errors.Count > 0)
{
return false;
}
else
{
return true;
}
}
public static String GenerateVBCode(CodeCompileUnit compileunit)
{
VBCodeProvider provider = new VBCodeProvider();
ICodeGenerator gen = provider.CreateGenerator();
String sourceFile;
if (provider.FileExtension[0] == ‘.’)
{
sourceFile = “HelloWorldvb” + provider.FileExtension;
}
else
{
sourceFile = “HelloWorldvb.” + provider.FileExtension;
}
IndentedTextWriter tw = new IndentedTextWriter( new StreamWriter(sourceFile, false), ” “);
gen.GenerateCodeFromCompileUnit(compileunit, tw,new CodeGeneratorOptions());
tw.Close();
return sourceFile;
}
public static bool CompileVBCode(String sourceFile, String exeFile)
{
VBCodeProvider provider = new VBCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add( “System.dll” );

cp.GenerateExecutable = true;
cp.OutputAssembly = exeFile;
cp.GenerateInMemory = false;
CompilerResults cr = compiler.CompileAssemblyFromFile(cp, sourceFile);
if(cr.Errors.Count > 0)
{
Console.WriteLine(“Errors building {0} into {1}”,
sourceFile, cr.PathToAssembly);
foreach(CompilerError ce in cr.Errors)
{
Console.WriteLine(” {0}”, ce.ToString());
Console.WriteLine();
}
}
else
{
Console.WriteLine(“Source {0} built into {1} successfully.”,
sourceFile, cr.PathToAssembly);
}
if (cr.Errors.Count > 0)
{
return false;
}
else
{
return true;
}
}
}
}

Advertisements

One thought on “Dynamically creating C# and VB code from CodeDOM

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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