SIGN UP MEMBER LOGIN:    
ARTICLE

Dynamic Code Compilation

Posted by Thiagarajan Alagarsamy Articles | C# Assemblies November 30, 2006
This article throws light on how a code can be built dynamically, compiled and run.
Reader Level:
Download Files:
 

Welcome to dynamic code compilation. This article throws light on how a code can be built dynamically, compiled and run. The scenario of dynamic code compilation may arise when you want an application to upgrade (or rewrite) another application.

Lets jump in to coding. The namespaces that we are going to use in this project are

  • System.CodeDom
  • System.CodeDom.Compiler
  • Microsoft.CSharp

The System.CodeDom namespace contains classes that can be used to represent the elements and structure of a source code document. The classes in this namespace can be used to model the structure of a source code document that can be output as source code in a supported language using the functionality provided by the System.CodeDom.Compiler namespace.

The Microsoft.CSharp namespace contains classes that support compilation and code generation using the C# language.

At first we need to instantiate the class CSharpCodeProvider. Then create a complier using CreateCompiler() function of CSharpCodeProvider class and assign to the interface ICodeCompiler.

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();

Then set the parameters for the compiler using CompilerParameters class. Set parameters like GenerateExecutable, OutputAssembly

 

System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = "Out.exe";


Now declare a String variable and write the source code for the new application/program. Now instantiate CompilerResults class with compiler parameters and the source code as below.

 

string sourcecode;
sourcecode="using System;namespace SampleApp{class Class1{[STAThread] static void Main(string[] args) { Console.WriteLine(\"I am born to live!\");Console.Read(); } }}";
CompilerResults results = icc.CompileAssemblyFromSource(parameters,sourcecode);


Now the code is compiled. If you want to run the compiled EXE, start the process by

Process.Start("Out.exe");

MOM.JPG

new_app.JPG

The source code is available for download at the top section of this page

Thats all folks!....

Happy coding

Login to add your contents and source code to this article
share this article :
post comment
 

I am sure the things been changed since it is created.
Present day, the ICodeCompiler is obselete and the CSharpCodeCompiler has the method CreateAssembleFromSource.
Also, in the above example we would need to use System.Diagnostics to refer to Process class.

Posted by Naresh M P Aug 27, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor