Click here to Skip to main content
Licence CPOL
First Posted 21 Dec 2005
Views 77,989
Downloads 867
Bookmarked 46 times

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

By | 18 Jan 2006 | Article
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

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTarget Framework 3? Pinmembernosuch771:26 11 Nov '11  
GeneralMy vote of 5 PinmemberBillWoodruff14:56 15 Oct '11  
GeneralMy vote of 5 PinmemberJonasSSH1:06 16 Jun '11  
GeneralAssembly Version number PinmemberAdriaan Davel21:39 6 Oct '10  
QuestionGenerateInAssembly = True ? Pinmemberdeostroll13:15 7 May '07  
GeneralGood one PinmemberMuammar©19:55 6 May '07  
Questioncompilation error on vs2005 PinmemberFrancesco Spegni22:09 17 Apr '07  
AnswerRe: compilation error on vs2005 PinmemberAndré Janus1:37 18 Apr '07  
hi,
 
first of all: thank your for your comment! Smile | :)
 
i wrote the article and the code a few month ago with .net 1.0 and vs2003. i thought newer versions would be compatible to that anyway (or even show an deprecated-message or something).
 
nowadays i don´t even work with .net and vs anymore, so i don´t want to change the article without knowing what´s going on in the .net-universe and what other changes should be done to article and code.
 
maybe you want to write a new article for run-time code generation...

GeneralLCG PinmemberHerbert Sauro5:27 28 Dec '05  
GeneralRe: LCG PinmemberexDreamDuck5:19 29 Dec '05  
GeneralRe: LCG PinmemberHerbert Sauro5:26 29 Dec '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 18 Jan 2006
Article Copyright 2005 by André Janus
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid