Click here to Skip to main content
Click here to Skip to main content

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

, 18 Jan 2006
Rate this:
Please Sign up or sign in to vote.
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:

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);

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)

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 Pinmemberginnas4-Nov-12 20:18 
Questionhow can we execute the code at run time in code DOM tech Pinmemberginnas15-Oct-12 18:51 
AnswerRe: how can we execute the code at run time in code DOM tech PinmemberJames Moore21-Oct-12 15:38 
GeneralRe: how can we execute the code at run time in code DOM tech Pinmemberginnas26-Oct-12 2:24 
QuestionGood One PinmemberSm.Abdullah6-Oct-12 20:12 
QuestionTarget Framework 3? Pinmembernosuch7711-Nov-11 1:26 
GeneralMy vote of 5 PinmemberBillWoodruff15-Oct-11 14:56 
GeneralMy vote of 5 PinmemberJonasSSH16-Jun-11 1:06 
GeneralAssembly Version number PinmemberAdriaan Davel6-Oct-10 21:39 
QuestionGenerateInAssembly = True ? Pinmemberdeostroll7-May-07 13:15 
GeneralGood one PinmemberMuammar©6-May-07 19:55 
Questioncompilation error on vs2005 PinmemberFrancesco Spegni17-Apr-07 22:09 
AnswerRe: compilation error on vs2005 PinmemberAndré Janus18-Apr-07 1:37 
GeneralLCG PinmemberHerbert Sauro28-Dec-05 5:27 
GeneralRe: LCG PinmemberexDreamDuck29-Dec-05 5:19 
GeneralRe: LCG PinmemberHerbert Sauro29-Dec-05 5:26 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 18 Jan 2006
Article Copyright 2005 by André Janus
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid