예제 #1
0
        public static Assembly Compile(string[] files)
        {
            var outputName = Mathf.Abs(DateTime.Now.GetHashCode()) + ".dll";
            var options    = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory   = false,
                OutputAssembly     = outputName
            };

            options.ReferencedAssemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies()
                                                  .Where(a => !a.IsDynamic)
                                                  .Select(a => a.Location)
                                                  .Where(s => !string.IsNullOrEmpty(s))
                                                  .ToArray());

            var compiler = new CodeCompiler();
            var result   = compiler.CompileAssemblyFromFileBatch(options, files);

            if (result.Errors.Count > 0)
            {
                Console.instance.Print("There were compilation errors.");
                foreach (CompilerError resultError in result.Errors)
                {
                    Console.instance.Print($"[{resultError.Line}]: {resultError.ErrorText}");
                }
            }

            return(result.CompiledAssembly);
        }