csscript.CompilerException.Create C# (CSharp) Method

Create() public static method

Creates the CompilerException instance from the specified compiler errors.
public static Create ( CompilerErrorCollection Errors, bool hideCompilerWarnings ) : CompilerException
Errors System.CodeDom.Compiler.CompilerErrorCollection The compiler errors.
hideCompilerWarnings bool if set to true hide compiler warnings.
return CompilerException
        public static CompilerException Create(CompilerErrorCollection Errors, bool hideCompilerWarnings)
        {
            StringBuilder compileErr = new StringBuilder();

            int errorCount = 0;

            foreach (CompilerError err in Errors)
            {
                if (!err.IsWarning)
                    errorCount++;

                if (err.IsWarning && hideCompilerWarnings)
                    continue;

                //compileErr.Append(err.ToString());
                compileErr.Append(err.FileName);
                compileErr.Append("(");
                compileErr.Append(err.Line);
                compileErr.Append(",");
                compileErr.Append(err.Column);
                compileErr.Append("): ");
                if (err.IsWarning)
                    compileErr.Append("warning ");
                else
                    compileErr.Append("error ");
                compileErr.Append(err.ErrorNumber);
                compileErr.Append(": ");
                compileErr.Append(err.ErrorText);
                compileErr.Append(Environment.NewLine);
            }
            CompilerException retval = new CompilerException(compileErr.ToString());
            retval.Data.Add("Errors", Errors);
            retval.ErrorCount = errorCount;
            return retval;
        }
    }

Usage Example

        // UnitTest
        // Make Surrogate scenario to compile conditionally
        // + check and delete the exe before building
        // + set Appartment state
        // + update all ExecutionClients incliding csslib
        // + when starting remove css and //x args
        //+ try to solve limitations with console Input redurectionlimi
        //+ ensure launcher is not build when building dll/exe without execution
        public string BuildSurrogateLauncher(string scriptAssembly, string tragetFramework, CompilerParameters compilerParams, ApartmentState appartmentState, string consoleEncoding)
        {
            //Debug.Assert(false);
#if !net4
            throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR.");
#else
            var provider = CodeDomProvider.CreateProvider("C#", new Dictionary <string, string> {
                { "CompilerVersion", tragetFramework }
            });

            compilerParams.OutputAssembly          = GetLauncherName(scriptAssembly);
            compilerParams.GenerateExecutable      = true;
            compilerParams.GenerateInMemory        = false;
            compilerParams.IncludeDebugInformation = false;

            try
            {
                Utils.FileDelete(compilerParams.OutputAssembly, true);
            }
            catch (Exception e)
            {
                throw new ApplicationException("Cannot build surrogate host application", e);
            }

            if (compilerParams.CompilerOptions != null)
            {
                compilerParams.CompilerOptions = compilerParams.CompilerOptions.Replace("/d:TRACE", "")
                                                 .Replace("/d:DEBUG", "");
            }

            if (!AppInfo.appConsole)
            {
                compilerParams.CompilerOptions += " /target:winexe";
            }

            string refAssemblies = "";
            string appartment    = "[STAThread]";
            if (appartmentState == ApartmentState.MTA)
            {
                appartment = "[" + appartmentState + "Thread]";
            }
            else if (appartmentState == ApartmentState.Unknown)
            {
                appartment = "";
            }

            foreach (string asm in compilerParams.ReferencedAssemblies)
            {
                if (File.Exists(asm)) //ignore GAC (not full path) assemblies
                {
                    refAssemblies += Assembly.ReflectionOnlyLoadFrom(asm).FullName + ":" + asm + ";";
                }
            }

            compilerParams.ReferencedAssemblies.Clear(); //it is important to remove all asms as they can have absolute path to the wrong CLR asms branch
            compilerParams.ReferencedAssemblies.Add("System.dll");

            foreach (var item in compilerParams.ReferencedAssemblies)
            {
                Debug.WriteLine(item);
            }

            string setEncodingSatement = "";

            if (string.Compare(consoleEncoding, Settings.DefaultEncodingName, true) != 0)
            {
                setEncodingSatement = "try { Console.OutputEncoding = System.Text.Encoding.GetEncoding(\"" + consoleEncoding + "\"); } catch {}";
            }

            string code = launcherCode
                          .Replace("${REF_ASSEMBLIES}", refAssemblies)
                          .Replace("${APPARTMENT}", appartment)
                          .Replace("${CONSOLE_ENCODING}", setEncodingSatement)
                          .Replace("${ASM_MANE}", Path.GetFileName(scriptAssembly));

            CompilerResults retval;

            compilerParams.IncludeDebugInformation = true;

            bool debugLauncher = false;
            if (debugLauncher)
            {
                compilerParams.CompilerOptions += " /d:DEBUG";

                string launcherFile = Environment.ExpandEnvironmentVariables(@"C:\Users\%USERNAME%\Desktop\New folder\script.launcher.cs");
                File.WriteAllText(launcherFile, code);
                retval = provider.CompileAssemblyFromFile(compilerParams, launcherFile);
            }
            else
            {
                retval = provider.CompileAssemblyFromSource(compilerParams, code);
            }

            foreach (CompilerError err in retval.Errors)
            {
                if (!err.IsWarning)
                {
                    throw CompilerException.Create(retval.Errors, true, false);
                }
            }

            CSSUtils.SetTimestamp(compilerParams.OutputAssembly, scriptAssembly);
            return(compilerParams.OutputAssembly);
#endif
        }
All Usage Examples Of csscript.CompilerException::Create