public IScriptCompilerResult Compile(string[] scriptText) { var scriptTextsWithHash = new CodeTextsWithHash(scriptText); var result = Altaxo.CodeEditing.CompilationHandling.CompilationServiceStatic.GetCompilation(scriptTextsWithHash, scriptTextsWithHash.Hash, Altaxo.Settings.Scripting.ReferencedAssemblies.All); if (result.CompiledAssembly != null) { return(new ScriptCompilerSuccessfulResult(scriptTextsWithHash, result.CompiledAssembly)); } else { return(new ScriptCompilerFailedResult(scriptTextsWithHash, result.Diagnostics.Select(diag => new CompilerDiagnostic(diag.Line, diag.Column, (DiagnosticSeverity)diag.Severity, diag.MessageText)))); } }
/// <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 IScriptCompilerResult Compile(string[] scriptText) { var scriptTextWithHash = new CodeTextsWithHash(scriptText); if (_compilerResults.TryGetValue(scriptTextWithHash.Hash, out var result)) { return(result); } var providerOptions = new Dictionary <string, string> { { "CompilerVersion", "v4.0" } }; var codeProvider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions); // For Visual Basic Compiler try this : //Microsoft.VisualBasic.VBCodeProvider var parameters = new CompilerParameters { GenerateInMemory = true, IncludeDebugInformation = true }; // parameters.OutputAssembly = this.ScriptName; // Add available assemblies including the application itself foreach (string loc in Settings.Scripting.ReferencedAssemblies.All.Select(ass => ass.Location)) { parameters.ReferencedAssemblies.Add(loc); } CompilerResults results; if (scriptText.Length == 1) { results = codeProvider.CompileAssemblyFromSource(parameters, scriptText[0]); } else { results = codeProvider.CompileAssemblyFromSource(parameters, scriptText); } if (results.Errors.Count > 0) { var errors = new List <ICompilerDiagnostic>(results.Errors.Count); foreach (CompilerError err in results.Errors) { errors.Add(new CompilerDiagnostic(err.Line, err.Column, err.IsWarning ? DiagnosticSeverity.Warning : DiagnosticSeverity.Error, err.ErrorText)); } result = new ScriptCompilerFailedResult(scriptTextWithHash, errors); _compilerResults.TryAdd(result); return(result); } else { result = new ScriptCompilerSuccessfulResult(scriptTextWithHash, results.CompiledAssembly); _compilerResults.TryAdd(result); return(result); } }
public ScriptCompilerFailedResult(CodeTextsWithHash scriptText, IEnumerable <ICompilerDiagnostic> compileErrors) { CodeText = scriptText ?? throw new ArgumentNullException(nameof(scriptText)); _compileErrors = compileErrors.ToImmutableArray(); }
public ScriptCompilerSuccessfulResult(CodeTextsWithHash codeText, Assembly assembly) { CodeText = codeText ?? throw new ArgumentNullException(nameof(codeText)); ScriptAssembly = assembly ?? throw new ArgumentNullException(nameof(assembly)); }