コード例 #1
0
        /// <summary>
        /// Tries to get a compilation result from an existing assembly.
        /// </summary>
        /// <param name="assembly">The compiled assembly.</param>
        /// <param name="result">Returns the compilation result corresponding to the assembly (always a successful compilation result).</param>
        /// <returns>True if the compulation result corresponding to this assembly could be found, otherwise, false.</returns>
        public bool TryGetValue(Assembly assembly, out ScriptCompilerSuccessfulResult result)
        {
            if (null == assembly)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            _lock.EnterReadLock();
            try
            {
                return(_compilerResultsByAssembly.TryGetValue(assembly, out result));
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
コード例 #2
0
            /// <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);
                }
            }