/// <summary> /// Compiles (multiple) code to assembly. /// </summary> /// <returns>The to assembly from code.</returns> /// <param name="code">Code.</param> /// <param name="assemblyName">Assembly name.</param> public Assembly CompileToAssemblyFromCode(string[] code, string assemblyName = "") { SetOutputAssemblyName(assemblyName); var result = compiler.CompileAssemblyFromSourceBatch(options, code); return(CheckCompileResult(result, assemblyName)); }
public static Assembly CompileSource(string[] sources, bool isSource, bool GenerateInMemory = true) { if (CodeCompiler == null) { CodeCompiler = new CSharpCompiler.CodeCompiler(); } var compilerparams = new CompilerParameters(); //add all references to assembly - need to use Assembly resolver for Dynamicly created //assemblies, as assembly.Location will fail for them foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { try { compilerparams.ReferencedAssemblies.Add(assembly.Location); } catch { if (DynamicAssemblyResolver.ContainsKey(assembly.FullName)) { compilerparams.ReferencedAssemblies.Add(assembly.GetName().FullName); } } } compilerparams.GenerateExecutable = false; compilerparams.GenerateInMemory = GenerateInMemory; AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => { if (DynamicAssemblyResolver.ContainsKey(e.Name)) { //UnityEngine.Debug.Log("resolved assembly for:" + e.Name); return(DynamicAssemblyResolver[e.Name]); } else { return(null); } }; // Compile the source CompilerResults result; if (isSource) { result = CodeCompiler.CompileAssemblyFromSourceBatch(compilerparams, sources); } else { result = CodeCompiler.CompileAssemblyFromFileBatch(compilerparams, sources); } if (result.CompiledAssembly != null) { if (!DynamicAssemblyResolver.ContainsKey(result.CompiledAssembly.FullName)) { DynamicAssemblyResolver.Add(result.CompiledAssembly.FullName, result.CompiledAssembly); } } if (result.Errors.Count > 0) { var msg = new StringBuilder(); foreach (CompilerError error in result.Errors) { msg.AppendFormat("Error ({0}): {1}\n", error.ErrorNumber, error.ErrorText); } throw new Exception(msg.ToString()); } // Return the assembly return(result.CompiledAssembly); }