Codedom example

The CodeDOM provides types that represent many common types of source code elements. You can design a program that builds a source code model using CodeDOM elements to assemble an object graph. This object graph can be rendered as source code using a CodeDOM code generator for a supported programming language. The CodeDOM can also be used to compile source code into a binary assembly.

Some common uses for the CodeDOM include:

> Templated code generation: generating code for ASP.NET, XML Web services client proxies, code wizards, designers, or other code-emitting mechanisms.

> Dynamic compilation: supporting code compilation in single or multiple languages.


private void compile(String filename){
	CompilerParameters options = new CompilerParameters();
	CompilerResults results = default(CompilerResults);
	Dictionary<string, string> providerOptions = new Dictionary<string, string>();
	providerOptions.Add("CompilerVersion", "v3.5");
	CSharpCodeProvider compiler = new CSharpCodeProvider(providerOptions);
	options.GenerateInMemory = false;
	options.GenerateExecutable = true;
	options.OutputAssembly = "myexecutable.exe";
	options.TreatWarningsAsErrors = false;
	options.WarningLevel = 3;
	options.CompilerOptions = "/platform:x86 /target:winexe /optimize+ /debug-";
	options.IncludeDebugInformation = false;
	if (compiler.Supports(GeneratorSupport.EntryPointMethod))
		options.MainClass = "MyNamespace.Program";
	results = compiler.CompileAssemblyFromFile(options, new String[] { filename });
	if (results.Errors.Count > 0)
		foreach (CompilerError error in results.Errors)
			Console.WriteLine("Error: " + error.ErrorText + "rnLine: " + error.Line);