CompilerError Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Represents a compiler error or warning.

Namespace:   System.CodeDom.Compiler
Assembly:  System (in System.dll)

[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public class CompilerError


Initializes a new instance of the CompilerError class.

System_CAPS_pubmethodCompilerError(String, Int32, Int32, String, String)

Initializes a new instance of the CompilerError class using the specified file name, line, column, error number, and error text.


Gets or sets the column number where the source of the error occurs.


Gets or sets the error number.


Gets or sets the text of the error message.


Gets or sets the file name of the source file that contains the code which caused the error.


Gets or sets a value that indicates whether the error is a warning.


Gets or sets the line number where the source of the error occurs.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Provides an implementation of Object's ToString method.(Overrides Object.ToString().)

CompilerError represents a compiler error or a warning that has been returned by the compiler.


This class contains an inheritance demand at the class level that applies to all members. A SecurityException is thrown when the derived class does not have full-trust permission. For details about inheritance demands, see Inheritance Demands.

The following example compiles a CodeDOM program graph and provides an example of how to programmatically access CompilerError data.

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace CompilerError_Example
    public class Class1
        static void Main(string[] args)			
            // Output some program information using Console.WriteLine.
            Console.WriteLine("This program compiles a CodeDOM program that incorrectly declares multiple data");
            Console.WriteLine("types to demonstrate handling compiler errors programmatically.");

            // Compile the CodeCompileUnit retrieved from the GetCompileUnit() method.
            CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();

            // Initialize a CompilerParameters with the options for compilation.
            string[] assemblies = new String[] {"System.dll"};
            CompilerParameters options = new CompilerParameters( assemblies, "output.exe");

            // Compile the CodeDOM graph and store the results in a CompilerResults.
            CompilerResults results = provider.CompileAssemblyFromDom(options, GetCompileUnit());

            // Compilation produces errors. Print out each error.
            Console.WriteLine("Listing errors from compilation: ");
                for( int i=0; i<results.Errors.Count; i++)

        public static CodeCompileUnit GetCompileUnit()
            // Create a compile unit to contain a CodeDOM graph.
            CodeCompileUnit cu = new CodeCompileUnit();

            // Create a namespace named TestSpace.
            CodeNamespace cn = new CodeNamespace("TestSpace");		

            // Declare a new type named TestClass.	
            CodeTypeDeclaration cd = new CodeTypeDeclaration("TestClass");

            // Declare a new member string field named TestField.
            CodeMemberField cmf = new CodeMemberField("System.String", "TestField");

            // Add the field to the type.

            // Declare a new member method named TestMethod.
            CodeMemberMethod cm = new CodeMemberMethod();
            cm.Name = "TestMethod";

            // Declare a string variable named TestVariable.
            CodeVariableDeclarationStatement cvd = new CodeVariableDeclarationStatement("System.String1", "TestVariable");

            // Cast the TestField reference expression to string and assign it to the TestVariable.
            CodeAssignStatement ca = new CodeAssignStatement(new CodeVariableReferenceExpression("TestVariable"), 
                new CodeCastExpression("System.String2", new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "TestField")));

            // This code can be used to generate the following code in C#:
            //            TestVariable = ((string)(this.TestField));


            // Add the TestMethod member to the TestClass type.

            // Add the TestClass type to the namespace.

            // Add the TestSpace namespace to the compile unit.
            return cu;


for deriving from the CompilerError class. Demand value: InheritanceDemand; PermissionSet: FullTrust.

.NET Framework
Available since 1.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top