|
/*code taken from: http://www.codeproject.com/csharp/RuntimeCompiling.asp*/
|
|
|
|
using System;
|
|
using System.Text;
|
|
using Microsoft.CSharp;
|
|
using System.CodeDom.Compiler;
|
|
using System.Reflection;
|
|
|
|
namespace TUG.Mogentes
|
|
{
|
|
/// <summary>
|
|
/// This class can be used to execute dynamic uncompiled code at runtime
|
|
/// This class is not exception safe, all function calls should be exception handled.
|
|
/// </summary>
|
|
public class CSharpCodeCompiler
|
|
{
|
|
/// <summary>
|
|
/// This is the main code execution function.
|
|
/// It allows for any compilable c# code to be executed.
|
|
/// </summary>
|
|
/// <param name="code">the code to be compiled then executed</param>
|
|
/// <param name="namespacename">the name of the namespace to be executed</param>
|
|
/// <param name="classname">the name of the class of the function in the code that you will execute</param>
|
|
/// <param name="functionname">the name of the function that you will execute</param>
|
|
/// <param name="isstatic">True if the function you will execute is static, otherwise false</param>
|
|
/// <param name="args">any parameters that must be passed to the function</param>
|
|
/// <returns>what ever the executed function returns, in object form</returns>
|
|
public static object ExecuteCode(Assembly asm, string namespacename, string classname,
|
|
string functionname, params object[] args)
|
|
{
|
|
// Assembly asm = BuildAssembly(code, new string[] {"System.dll", "SepiasTool.exe"});
|
|
object instance = null;
|
|
object returnval = null;
|
|
Type type = asm.GetType(namespacename + "." + classname);
|
|
System.Reflection.MethodInfo method = type.GetMethod(functionname);
|
|
if (!method.IsStatic)
|
|
{
|
|
instance = asm.CreateInstance(namespacename + "." + classname);
|
|
}
|
|
returnval = method.Invoke(instance, args);
|
|
return returnval;
|
|
}
|
|
|
|
/// <summary>
|
|
/// This private function builds the assembly file into memory after compiling the code
|
|
/// </summary>
|
|
/// <param name="code">C# code to be compiled</param>
|
|
/// <returns>the compiled assembly object</returns>
|
|
///
|
|
public static Assembly BuildAssembly(string [] code, string[] referencedassemblies)
|
|
{
|
|
CodeDomProvider provider = new CSharpCodeProvider();
|
|
CompilerParameters compilerparams = new CompilerParameters();
|
|
compilerparams.GenerateExecutable = false;
|
|
compilerparams.GenerateInMemory = true;
|
|
compilerparams.ReferencedAssemblies.AddRange(referencedassemblies);
|
|
CompilerResults results = provider.CompileAssemblyFromSource(compilerparams, code);
|
|
if (results.Errors.HasErrors)
|
|
{
|
|
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
|
|
foreach (CompilerError error in results.Errors)
|
|
{
|
|
errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText);
|
|
}
|
|
throw new UlyssesCompileException(errors.ToString());
|
|
}
|
|
else
|
|
{
|
|
return results.CompiledAssembly;
|
|
}
|
|
}
|
|
}
|
|
}
|