Dynamic Compilation

It is ridiculously simple to compile code dynamically using the .NET Framework. Most of the code is merely setting up the parameters you need to pass to the compiler. Once that is done, basically, you have a 2-liner on your hands. We are using the technique to provide formulas to a bidding system that we are working on. Essentially, the code looks like this:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace AppUtils
{
    public class Compiler
    {
        private static readonly CompilerParameters parameters;

        static Compiler()
        {
            parameters = CreateCompilerParameters();
        }

        public static CompilerResults Compile(string code)
        {
            var provider = new CSharpCodeProvider();
            return provider.CompileAssemblyFromSource(parameters, code);
        }

        private static CompilerParameters CreateCompilerParameters()
        {
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            var options = new CompilerParameters
                              {GenerateInMemory = true, GenerateExecutable = false, IncludeDebugInformation = true};
            foreach (Assembly loadedAssemblies in assemblies)
            {
                options.ReferencedAssemblies.Add(loadedAssemblies.Location);
                foreach (AssemblyName reference in loadedAssemblies.GetReferencedAssemblies())
                {
                    Assembly referencedAssembly = Assembly.Load(reference);
                    options.ReferencedAssemblies.Add(referencedAssembly.Location);
                }
            }
            return options;
        }
    }
}
Our "formulas" are just code with a friendly name (e.g. "Turnkey Days + 1"). The extensibility points in our system are maintained by developers, so this is not a problem. However, in situations where I would want to provide the user the ability to enter formulas, I still think it would be easy to write some kind of pre-processor that could expand simple formulas into C# which could then be forwarded to the compiler at runtime.
June 16, 2008 10:34 by JP
E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Related posts

Comments

June 22. 2008 08:14

pingback

Pingback from rtipton.wordpress.com

Weekly Link Post 47 « Rhonda Tipton’s WebLog

rtipton.wordpress.com

June 22. 2008 11:07

pingback

Pingback from weblogs.asp.net

Interesting Finds: 2008.06.23 - gOODiDEA.NET

weblogs.asp.net

July 6. 2008 10:33

Chad Myers

NOTE: If you're just doing simple expressions and basic logic-type stuff (i.e. if( TurnkeyDays + 1 > Customer.AllowedTotalDays && Customer.HasContract) ) then you might consider using Expression Tree composition in .NET 3.5. Check out Expression.AndAlso(), Expression.GreaterThan, etc, etc). It will give you most of the functionality you're looking for without having to generate dynamic assemblies and some of the hassle that creates.

Chad Myers

Comments are closed