/// <summary> /// Does the compilation of the script into an assembly. The assembly is stored together with /// the read-only source code and returned as result. As list of compiled source codes is maintained by this class. /// If you provide a text that was already compiled before, the already compiled assembly is returned instead /// of a freshly compiled assembly. /// </summary> /// <returns>True if successfully compiles, otherwise false.</returns> public static IScriptCompilerResult Compile(string[] scriptText, out string[] errors) { string scriptTextHash = ScriptCompilerResult.ComputeScriptTextHash(scriptText); if (_compilerResultsByTextHash.Contains(scriptTextHash)) { errors = null; return((ScriptCompilerResult)_compilerResultsByTextHash[scriptTextHash]); } Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); // For Visual Basic Compiler try this : //Microsoft.VisualBasic.VBCodeProvider System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.IncludeDebugInformation = true; // parameters.OutputAssembly = this.ScriptName; // Add available assemblies including the application itself foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { if (!(asm is System.Reflection.Emit.AssemblyBuilder) && asm.Location != null && asm.Location != String.Empty) { parameters.ReferencedAssemblies.Add(asm.Location); } } CompilerResults results; if (scriptText.Length == 1) { results = codeProvider.CompileAssemblyFromSource(parameters, scriptText[0]); } else { results = codeProvider.CompileAssemblyFromSource(parameters, scriptText); } if (results.Errors.Count > 0) { errors = new string[results.Errors.Count]; int i = 0; foreach (CompilerError err in results.Errors) { errors[i++] = err.ToString(); } return(null); } else { ScriptCompilerResult result = new ScriptCompilerResult(scriptText, scriptTextHash, results.CompiledAssembly); _compilerResultsByTextHash.Add(scriptTextHash, result); _compilerResultsByAssembly.Add(result.ScriptAssembly, result); errors = null; return(result); } }
/// <summary> /// Computes the Script text hash of a single script text. /// </summary> /// <param name="scriptText">The script text.</param> /// <returns>A hash string which unique identifies the script text.</returns> public static string ComputeScriptTextHash(string scriptText) { return(ScriptCompilerResult.ComputeScriptTextHash(new string[] { scriptText })); }