Click here to Skip to main content
11,922,788 members (49,749 online)
Click here to Skip to main content
Add your own
alternative version


91 bookmarked

Runtime Compilation (A .NET eval statement)

, 13 Jan 2006
Rate this:
Please Sign up or sign in to vote.
An article on a new library, allowing easy, language-independent access to runtime compilation.


There are several languages out there (primarily interpreted languages) which provide a convenient eval statement or function. These usually accept simple fragments of code and execute them on the fly, often used for mathematical expressions. .NET leaves this useful feature out of its BCL (Base Class Library), and buries access to its compiler under incomplete documentation. I was working on a project of my own when I realized that I needed some form of runtime compilation, and went off on a tangent to build an easy-to-use library (also adding full XML documentation).

Using the code

The library is centered around the Eval class, which contains the static methods that access the compiler.

The methods provided are as follows:

public static AssemblyResults CreateAssembly(ICodeCompiler compiler,
                                          string assemblySource,
                                          CompilerParameters options,
                                          Language language);
public static TypeResults CreateType(ICodeCompiler compiler,
                                     string typeSource,
                                     string typeName,
                                     CompilerParameters options,
                                     Language language);
public static MethodResults CreateMethod(ICodeCompiler compiler,
                                         string methodSource,
                                         string methodName,
                                         CompilerParameters options,
                                         Language language);

public static AssemblyResults CreateVirtualAssembly(ICodeCompiler compiler,
                                         string assemblySource,
                                         bool debug,
                                         Language language,
                                         params string[] references);
public static TypeResults CreateVirtualType(ICodeCompiler compiler,
                                         string typeSource,
                                         string typeName,
                                         Language language,
                                         bool debug,
                                         params string[] references);
public static MethodResults CreateVirtualMethod(ICodeCompiler compiler,
                                         string methodSource,
                                         string methodName,
                                         Language language,
                                         bool debug,
                                         params string[] references);

Each method compiles the source provided, loads the result into memory, and returns a reference to an object wrapping the final result (along with any warnings generated by the compiler). If any errors are generated during the compilation, a CompilationException is thrown, which contains the compiler's errors. The "Virtual" group of methods stores the results of compilation directly to memory, using a pre-generated set of CompilerParameters.

The Language class and its subclasses provide information about an individual language to the Eval class, exposing methods that generate various language-specific segments of the code which are necessary in the CreateType and CreateMethod methods of the Eval class.

The result classes each expose the actual result through a property named by the type of the result (respectively through the Assembly, Type, and Method properties), as well as the collection of warnings generated during compilation (through the Warnings property). In addition, each class provides indirect access to some of the more commonly used abilities of its underlying result (each of which acts as described in its XML documentation).

The AssemblyResult class provides the following method:

public TypeResults GetType(string typeName, bool throwOnError);

The TypeResults class provides the following methods:

public MethodResults GetMethod(string name);
public MethodResults GetMethod(string name, 
                            params Type[] paramTypes);

public object Instantiate(params object[] parameters);

Finally, the MethodResults class provides the following method:

public object Invoke(params object[] parameters);

This method automatically creates an instance of the declaring class on which to invoke the wrapped method, allowing non-static methods to be invoked as if they were static.

Points of interest

Microsoft included nearly everything that I needed to write this library in their standard .NET Framework, except the final wrapper. If you find that you need something beyond the capabilities of my library, the System.CodeDom.Compiler and System.Reflection namespaces should provide most of what you need.

To do

More than a few things, most likely... Currently only one update comes to mind. If there is any demand for it, I will make the results of my runtime compilation interface unloadable from memory by using AppDomains (inspired by and probably borrowing from the DevX article Dynamically Executing Code in .NET).

This is my first article posted, so I'd appreciate any feedback. E-mail feedback is accepted, but I'd prefer feedback through the message board provided by the CodeProject.


  • 12th January, 2006
    • Updated code (again) to fix a bug with referenced libraries and an issue with in-memory compilation. Both features should now work as intended.
  • 6th December, 2003
    • Refactored code (added Language class and subclasses, updated Eval class) in order to make the Eval class work fully with languages other than C#, added VB.NET example, and updated the article to reflect these changes.
  • 14th January, 2003
    • Updated code to fix minor bugs with referenced libraries.
  • 11th January, 2003
    • Zip files finally corrected (extensive paths removed).
  • 2nd January, 2003
    • Example project, screenshot, and To do added, a few bugs fixed.
  • 31st December, 2002:
    • Article first posted.
    • ~5:31 PM EST: <pre> blocks fixed (Sorry!).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Eric Astor
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSome Query Pin
yashvi_rsy1-Oct-14 19:55
memberyashvi_rsy1-Oct-14 19:55 
GeneralMy vote of 5 Pin
BillWoodruff2-Jun-13 3:05
memberBillWoodruff2-Jun-13 3:05 
GeneralMy vote of 5 Pin
Member 17933029-Apr-13 5:12
memberMember 17933029-Apr-13 5:12 
GeneralGood job! Pin
Shane Story3-Sep-10 3:37
memberShane Story3-Sep-10 3:37 
GeneralMy vote of 5 Pin
Prasenjit Purohit6-Jul-10 20:28
memberPrasenjit Purohit6-Jul-10 20:28 
Generalnice article Pin
niuzj30-Nov-09 21:30
memberniuzj30-Nov-09 21:30 
GeneralByRef Arguments Pin
Member 28930215-Sep-08 4:33
memberMember 28930215-Sep-08 4:33 
GeneralPassing an array to the Method Pin
MisterT9917-Oct-07 7:12
memberMisterT9917-Oct-07 7:12 
Generalcompile error on MC++ VS003 Pin
clee005-Jul-06 13:25
memberclee005-Jul-06 13:25 
NewsDynamic Code Pin
Primadi6-Jun-06 1:03
memberPrimadi6-Jun-06 1:03 
QuestionReference COM-Object? Pin
dEUs[CPP]5-May-06 1:08
memberdEUs[CPP]5-May-06 1:08 

is it possible to somehow create the interop.dll needed for COM-Objects on the fly, too? So the user just tells me "I need want to use the COM-Object with this and this GUID" and I internally create the interop for him and add it to the compiler-references.

QuestionInclude current namespace? Pin
Mellmountain24-Mar-06 1:21
memberMellmountain24-Mar-06 1:21 
AnswerRe: Include current namespace? Pin
Mellmountain29-Mar-06 3:07
memberMellmountain29-Mar-06 3:07 
QuestionCompile multiple language source at same time? Pin
FocusedWolf4-Mar-06 7:07
memberFocusedWolf4-Mar-06 7:07 
QuestionUsing runtime compilation in a multi-threaded application Pin
x-cubed22-Feb-06 10:32
memberx-cubed22-Feb-06 10:32 
AnswerRe: Using runtime compilation in a multi-threaded application Pin
Eric Astor22-Feb-06 10:47
memberEric Astor22-Feb-06 10:47 
AnswerRe: Using runtime compilation in a multi-threaded application Pin
x-cubed22-Feb-06 12:11
memberx-cubed22-Feb-06 12:11 
GeneralYaaay... Pin
Ross Presser13-Jan-06 4:36
memberRoss Presser13-Jan-06 4:36 
GeneralReferencing external assemblies Pin
Eric Astor12-Jan-06 5:05
memberEric Astor12-Jan-06 5:05 
GeneralRe: Referencing external assemblies Pin
Eric Astor12-Jan-06 5:22
memberEric Astor12-Jan-06 5:22 
Generalinclude dll Pin
[ac]Sebastian12-Apr-05 6:13
member[ac]Sebastian12-Apr-05 6:13 
GeneralFeature suggestion: namespaces Pin
steve willer6-Aug-04 5:37
membersteve willer6-Aug-04 5:37 
GeneralSpecify output directory Pin
adlionelC2-Jun-04 7:55
memberadlionelC2-Jun-04 7:55 
GeneralRe: Specify output directory Pin
adlionelC2-Jun-04 23:12
memberadlionelC2-Jun-04 23:12 
GeneralRe: Specify output directory Pin
Eric Astor3-Jun-04 1:53
memberEric Astor3-Jun-04 1:53 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151125.1 | Last Updated 13 Jan 2006
Article Copyright 2002 by Eric Astor
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid