Click here to Skip to main content
14,574,685 members

Run-Time Code Generation I: Compile C#-Code using Microsoft.CSharp and System.CodeCom.Compiler

Rate this:
3.50 (19 votes)
Please Sign up or sign in to vote.
3.50 (19 votes)
18 Jan 2006CPOL
Compile C#-Code using Microsoft.CSharp and System.CodeCom.Compiler


.NET provides mechanisms to compile source code and to use the generated assemblies within an application. Using these features, you can make applications more adaptive and more flexible.

The following article shows how to compile C#-code using Microsoft.CSharp and System.CodeDom.

Necessary Using Statements

Before you start, add the following using statements to your application to ease the use of the namespaces:

using Microsoft.CSharp;
using System.CodeDom.Compiler;

Getting Started

The first step is to instantiate a CSharpCodeProvider and create a compiler with that CodeProvider:

CSharpCodeProvider myCodeProvider = new CsharpCodeProvider();
CodeCompiler myCodeCompiler = myCodeProvider.CreateCompiler();

Setting Parameters

Before starting the compilation, there are various parameters to influence the compilation and its results. An initial parameter is the Reference Assemblies array, which defines the assemblies you can use within your code. Mandatory is the System.dll assembly with the core .NET classes. If you want to use run-time code generation within an ASP.NET application, for example, you have to add System.Web.dll. And of course, you have to set the name of the assembly you want to generate.

String [] referenceAssemblies = {"System.dll"};
string myAssemblyName = „myAssembly.dll";

With these basic parameters, you can create the CompilerParameters object.

CompilerParameters myCompilerParameters = 
 new CompilerParameters(referenceAssemblies, myAssemblyName);

After that, you can choose if your assembly should be an executable file or not – the default is false, so the compiler creates a class library. Another useful option is to generate the assembly in memory or on hard disk – the default is false, so the compiler stores your assembly in the applications directory.

myCompilerParameters.GenerateExecutable = false;
myCompilerParameters.GenerateInMemory = false;

Compiler Results

After starting the compiler with the CompilerParameters object and of course the C# code you want to compile, you get a CompilerResults object, which helps you to check whether the compilation has been successful or not.

String CsharpSourceCode = „...";
CompilerResults myCompilerResults = 
 myCodeCompiler.CompileAssemblyFromSource(myCompilerParameters, CSharpSourceCode);


With a few lines of code, you make your application compile source code and create an assembly out of it. But note that you have to provide error-free C# code or check the compiler results before you use the generated assembly.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

André Janus
Web Developer
Germany Germany
I am currently student at the university of Karlsruhe and working on my master thesis at the IT-Management and Web Engineering Research Group within the institute of telematics.

Comments and Discussions

Questionhow to append more functions inside the code for compilation and execution at run time Pin
ginnas4-Nov-12 20:18
Memberginnas4-Nov-12 20:18 
Questionhow can we execute the code at run time in code DOM tech Pin
ginnas15-Oct-12 18:51
Memberginnas15-Oct-12 18:51 
AnswerRe: how can we execute the code at run time in code DOM tech Pin
James Moore21-Oct-12 15:38
MemberJames Moore21-Oct-12 15:38 
GeneralRe: how can we execute the code at run time in code DOM tech Pin
ginnas26-Oct-12 2:24
Memberginnas26-Oct-12 2:24 
QuestionGood One Pin
Sm.Abdullah6-Oct-12 20:12
MemberSm.Abdullah6-Oct-12 20:12 
QuestionTarget Framework 3? Pin
nosuch7711-Nov-11 1:26
professionalnosuch7711-Nov-11 1:26 
GeneralMy vote of 5 Pin
BillWoodruff15-Oct-11 14:56
mveBillWoodruff15-Oct-11 14:56 
GeneralMy vote of 5 Pin
JonasSSH16-Jun-11 1:06
MemberJonasSSH16-Jun-11 1:06 
GeneralAssembly Version number Pin
Adriaan Davel6-Oct-10 21:39
MemberAdriaan Davel6-Oct-10 21:39 
QuestionGenerateInAssembly = True ? Pin
deostroll7-May-07 13:15
Memberdeostroll7-May-07 13:15 
GeneralGood one Pin
Muammar©6-May-07 19:55
Member Muammar©6-May-07 19:55 
Questioncompilation error on vs2005 Pin
Francesco Spegni17-Apr-07 22:09
MemberFrancesco Spegni17-Apr-07 22:09 
AnswerRe: compilation error on vs2005 Pin
André Janus18-Apr-07 1:37
MemberAndré Janus18-Apr-07 1:37 
GeneralLCG Pin
Herbert Sauro28-Dec-05 5:27
MemberHerbert Sauro28-Dec-05 5:27 
GeneralRe: LCG Pin
DeltaEngine29-Dec-05 5:19
professionalDeltaEngine29-Dec-05 5:19 
GeneralRe: LCG Pin
Herbert Sauro29-Dec-05 5:26
MemberHerbert Sauro29-Dec-05 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 21 Dec 2005


51 bookmarked