Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / parser / invokeSharpCompiler.cs @ 3

1
/*code taken from: http://www.codeproject.com/csharp/RuntimeCompiling.asp*/
2

    
3
using System;
4
using System.Text;
5
using Microsoft.CSharp;
6
using System.CodeDom.Compiler;
7
using System.Reflection;
8

    
9
namespace TUG.Mogentes
10
{
11
    /// <summary>
12
    /// This class can be used to execute dynamic uncompiled code at runtime
13
    /// This class is not exception safe, all function calls should be exception handled.
14
    /// </summary>
15
    public class CSharpCodeCompiler
16
    {
17
        /// <summary>
18
        /// This is the main code execution function.
19
        /// It allows for any compilable c# code to be executed.
20
        /// </summary>
21
        /// <param name="code">the code to be compiled then executed</param>
22
        /// <param name="namespacename">the name of the namespace to be executed</param>
23
        /// <param name="classname">the name of the class of the function in the code that you will execute</param>
24
        /// <param name="functionname">the name of the function that you will execute</param>
25
        /// <param name="isstatic">True if the function you will execute is static, otherwise false</param>
26
        /// <param name="args">any parameters that must be passed to the function</param>
27
        /// <returns>what ever the executed function returns, in object form</returns>
28
        public static object ExecuteCode(Assembly asm, string namespacename, string classname,
29
            string functionname, params object[] args)
30
        {
31
            // Assembly asm = BuildAssembly(code, new string[] {"System.dll", "SepiasTool.exe"});
32
            object instance = null;
33
            object returnval = null;
34
            Type type = asm.GetType(namespacename + "." + classname);
35
            System.Reflection.MethodInfo method = type.GetMethod(functionname);
36
            if (!method.IsStatic)
37
            {
38
                instance = asm.CreateInstance(namespacename + "." + classname);
39
            }
40
            returnval = method.Invoke(instance, args);
41
            return returnval;
42
        }
43

    
44
        /// <summary>
45
        /// This private function builds the assembly file into memory after compiling the code
46
        /// </summary>
47
        /// <param name="code">C# code to be compiled</param>
48
        /// <returns>the compiled assembly object</returns>
49
        /// 
50
        public static Assembly BuildAssembly(string [] code, string[] referencedassemblies)
51
        {
52
            CodeDomProvider provider = new CSharpCodeProvider();
53
            CompilerParameters compilerparams = new CompilerParameters();
54
            compilerparams.GenerateExecutable = false;
55
            compilerparams.GenerateInMemory = true;
56
            compilerparams.ReferencedAssemblies.AddRange(referencedassemblies);
57
            CompilerResults results = provider.CompileAssemblyFromSource(compilerparams, code);
58
            if (results.Errors.HasErrors)
59
            {
60
                StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
61
                foreach (CompilerError error in results.Errors)
62
                {
63
                    errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText);
64
                }
65
                throw new UlyssesCompileException(errors.ToString());
66
            }
67
            else
68
            {
69
                return results.CompiledAssembly;
70
            }
71
        }
72
    }
73
}