Share via


Create Assembly in-memory and run it...

This has to do with CodeDom and Microsoft.CSharp namespace. The following code will let you compile and run code in memory, IMHO - perfect for scripting engine needs:

using System;
using Specialized = System.Collections.Specialized;
using Reflection = System.Reflection;
using CSharp = Microsoft.CSharp;
using CodeDom = System.CodeDom.Compiler;

public sealed class TestCompile
{

static string ScriptCodeToCompileInMem = "public class Script {public void ScriptExecute(){System.Console.WriteLine(123);} }";
public static void Main()
{
TestCompile tc = new TestCompile();
tc.Execute(TestCompile.ScriptCodeToCompileInMem);
}

public void Execute(string scriptCode)
{
string [] source = new string[1];
source[0] = scriptCode;
CSharp.CSharpCodeProvider cscp = new CSharp.CSharpCodeProvider();
this.Compile(cscp, source[0]);
}

private void Compile(CodeDom.CodeDomProvider provider, string source)
{
CodeDom.CompilerParameters param = new CodeDom.CompilerParameters();
param.GenerateExecutable = false;
param.IncludeDebugInformation = false;
param.GenerateInMemory = true;
CodeDom.ICodeCompiler cc = provider.CreateCompiler();
CodeDom.CompilerResults cr = cc.CompileAssemblyFromSource(param, source);
Specialized.StringCollection output = cr.Output;
if(cr.Errors.Count !=0)
{
System.Console.WriteLine("Error invoking scripts.");
CodeDom.CompilerErrorCollection es = cr.Errors;
foreach(CodeDom.CompilerError s in es)
System.Console.WriteLine(s.ErrorText);
}
else
{
object o = cr.CompiledAssembly.CreateInstance("Script");
System.Type type = o.GetType();
type.InvokeMember ("ScriptExecute",
            Reflection.BindingFlags.InvokeMethod |
Reflection.BindingFlags.Default, null, o, null);
}
}
}

This posting is provided "AS IS" with no warranties, and confers no rights.

Comments

  • Anonymous
    January 31, 2004
    I think this abilty can be useful if you think about in the context of creating self-healing applications as well as in a scripting context.
  • Anonymous
    January 31, 2004
    I wouldn't recommend C#/.Net as a scripting environment to anyone.

    Sure, the C# compiler is intrinsic to the runtime, and your code allows you to compile and run arbitrary C# code in the context of your application.

    But getting rid of the new assembly once you have created it is not easy. You have to either do your compile in a separate AppDomain or load it into a new AppDomain from disk.

    Once you are finished with it, you have to unload the AppDomain to get rid of the code or your application will grow to consume all available memory. The final problem then is calling your new code across AppDomains.

    The overhead of AppDomain management is daunting, even to just understand, and the time it takes to compile and save an assembly dll is excruciating.

    I've been writing systems that create dynamic functionality (perl modules, c++ and delphi dlls, and now C# assemblies) for some time and .Net is the worst platform for plugins and scripting I have ever worked in.

    /rr
  • Anonymous
    January 31, 2004
    The comment has been removed
  • Anonymous
    April 27, 2004
    The comment has been removed
  • Anonymous
    June 14, 2004
    The comment has been removed