Monday, June 16, 2008

Dynamic Compilation

by J.P.

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()
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            var options = new CompilerParameters
            {
                GenerateInMemory = true,
                GenerateExecutable = false,
                IncludeDebugInformation = true
            };

            foreach (var loadedAssemblies in assemblies)
            {
                options.ReferencedAssemblies.Add(loadedAssemblies.Location);

                foreach (var reference in loadedAssemblies.GetReferencedAssemblies())
                {
                    var 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.

Related posts

Comments

6/22/2008 8:14:40 AM

pingback

Pingback from rtipton.wordpress.com

Weekly Link Post 47 « Rhonda Tipton’s WebLog

rtipton.wordpress.com

6/22/2008 11:07:05 AM

pingback

Pingback from weblogs.asp.net

Interesting Finds: 2008.06.23 - gOODiDEA.NET

weblogs.asp.net

7/6/2008 10:33:06 AM

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 us

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/28/2008 1:18:36 AM