Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
3.50/5 (19 votes)
18 Jan 2006CPOL2 min read 162.1K   2.6K   51   16
Compile C#-Code using Microsoft.CSharp and System.CodeCom.Compiler

Introduction

.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:

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

Getting Started

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

C#
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.

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

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

C#
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.

C#
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.

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

Conclusion

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.

References

License

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


Written By
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
ginnas4-Nov-12 20:18 
Questionhow can we execute the code at run time in code DOM tech Pin
ginnas15-Oct-12 18:51
ginnas15-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
James Moore21-Oct-12 15:38 
You add the compiled assembly to your project, good execution of this method would be the RunUO emulator. It features a Core/Distro setup. The core is a Server.exe that will compile all the distro code at run time. Pretty nifty.
GeneralRe: how can we execute the code at run time in code DOM tech Pin
ginnas26-Oct-12 2:24
ginnas26-Oct-12 2:24 
QuestionGood One Pin
Sm.Abdullah6-Oct-12 20:12
Sm.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
professionalBillWoodruff15-Oct-11 14:56 
GeneralMy vote of 5 Pin
JonasSSH16-Jun-11 1:06
JonasSSH16-Jun-11 1:06 
GeneralAssembly Version number Pin
Adriaan Davel6-Oct-10 21:39
Adriaan Davel6-Oct-10 21:39 
QuestionGenerateInAssembly = True ? Pin
deostroll7-May-07 13:15
deostroll7-May-07 13:15 
GeneralGood one Pin
Muammar©6-May-07 19:55
Muammar©6-May-07 19:55 
Questioncompilation error on vs2005 Pin
Francesco Spegni17-Apr-07 22:09
Francesco Spegni17-Apr-07 22:09 
AnswerRe: compilation error on vs2005 Pin
André Janus18-Apr-07 1:37
André Janus18-Apr-07 1:37 
GeneralLCG Pin
Herbert Sauro28-Dec-05 5:27
Herbert 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
Herbert 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.