This is a mirror of official site: http://jasper-net.blogspot.com/

Programmatically Invoke the C# Compiler

| Tuesday, August 10, 2010
Imagine you have just generated some code using a code generation technique, wouldn’t it be really cool to then programmatically call the C# language compiler and generate assemblies from the generated code?

Let’s imagine we used an Xslt transformation to generate some code:

   XslCompiledTransform transform = new XslCompiledTransform();
   transform.Load("Program.xslt");
   transform.Transform("Program.xml", "Program.cs");

Using the CSharpCodeProvider class we can now programmatically call the C# compiler to generate the executable assembly. We’ll begin by advising the compiler of some options we’d like it to use when compiling our code using the CompilerParameters class.

   CompilerParameters parameters = new CompilerParameters();

We’ll specify that the assembly to be created should be an executable and we’ll also specify such things as whether debug information should be included, the warning level, whether warnings should be treated as errors etc.

   parameters.GenerateExecutable = true;
   parameters.IncludeDebugInformation = true;
   parameters.GenerateInMemory = false;
   parameters.TreatWarningsAsErrors = true;
   parameters.WarningLevel = 3;
   parameters.CompilerOptions = "/optimize";
   parameters.OutputAssembly = "Program.exe";

Read more: Doug Holland - An Architects Perspective

Posted via email from .NET Info

0 comments: