예제 #1
0
파일: Compiler.cs 프로젝트: jbowwww/JGL
        /// <summary>
        /// Compiles the C sharp code.
        /// </summary>
        /// <returns>A <see cref="System.CodeDom.Compiler.CompilerResults"/></returns>
        /// <param name="source">Source code string(s)</param>
        public CompilerResults CompileCode(string[] source, string[] referencePaths)
        {
            Trace.Log(TraceEventType.Information, "source=string({0}), referencePaths=string({1})", source.Length, referencePaths.Length);

            CSharpCodeProvider provider = new CSharpCodeProvider();
            Trace.Log(TraceEventType.Verbose, "CodeProvider: {0}", provider.GetType().Name);

            // Build the parameters for source compilation.
            CompilerParameters cp = new CompilerParameters();
            cp.ReferencedAssemblies.AddRange(referencePaths);
            cp.GenerateExecutable = true;
            cp.OutputAssembly = "DynamicCodeTests-Scene.dll";
            cp.GenerateInMemory = false;
            Trace.Log(TraceEventType.Verbose, "Compiler Parameters = {{ {0} }}", ParamsToString(cp));

            // Invoke compilation.
            Results = provider.CompileAssemblyFromSource(cp, source);

            if (Results.Errors.Count > 0)
            {
                // Display compilation errors.
                string errorSummary = string.Format("Compilation errors: {0}", Results.Errors.Count.ToString());
                Trace.Log(TraceEventType.Information, errorSummary);
                foreach (CompilerError ce in Results.Errors)
                    Trace.Log(TraceEventType.Verbose, ce.ToString());
            }
            else
            {
                Trace.Log(TraceEventType.Verbose, "Source built {0}successfully.", cp.GenerateExecutable ? string.Concat("into ", Results.PathToAssembly, " ") : "");
            }
            return Results;
        }