![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
General
Intermediate
Runtime Compilation (A .NET eval statement)By Eric AstorAn article on a new library, allowing easy, language-independent access to runtime compilation. |
C#, VB.NET 1.0, Win2K, WinXP, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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).
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.
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.
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.
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. <pre> blocks fixed (Sorry!).
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 13 Jan 2006 Editor: Rinish Biju |
Copyright 2002 by Eric Astor Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |