This documentation is archived and is not being maintained.

CompilerError Class

Represents a compiler error or warning.

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

[SerializableAttribute]
[PermissionSetAttribute(SecurityAction::InheritanceDemand, Name = L"FullTrust")]
public ref class CompilerError

The CompilerError type exposes the following members.

  NameDescription
Public methodCompilerError()Initializes a new instance of the CompilerError class.
Public methodCompilerError(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.
Top

  NameDescription
Public propertyColumnGets or sets the column number where the source of the error occurs.
Public propertyErrorNumberGets or sets the error number.
Public propertyErrorTextGets or sets the text of the error message.
Public propertyFileNameGets or sets the file name of the source file that contains the code which caused the error.
Public propertyIsWarningGets or sets a value that indicates whether the error is a warning.
Public propertyLineGets or sets the line number where the source of the error occurs.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringProvides an implementation of Object's ToString method. (Overrides Object::ToString().)
Top

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

NoteNote

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.dll>

using namespace System;
using namespace System::CodeDom;
using namespace System::CodeDom::Compiler;
using namespace Microsoft::CSharp;
CodeCompileUnit^ GetCompileUnit()
{

   // Create a compile unit to contain a CodeDOM graph.
   CodeCompileUnit^ cu = gcnew CodeCompileUnit;

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

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

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

   // Add the field to the type.
   cd->Members->Add( cmf );

   // Declare a new member method named TestMethod.
   CodeMemberMethod^ cm = gcnew CodeMemberMethod;
   cm->Name = "TestMethod";

   // Declare a string variable named TestVariable.
   CodeVariableDeclarationStatement^ cvd = gcnew CodeVariableDeclarationStatement( "System.String1","TestVariable" );
   cm->Statements->Add( cvd );

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

   // This code can be used to generate the following code in C#:
   //            TestVariable = ((string)(this.TestField));
   cm->Statements->Add( ca );

   // Add the TestMethod member to the TestClass type.
   cd->Members->Add( cm );

   // Add the TestClass type to the namespace.
   cn->Types->Add( cd );

   // Add the TestSpace namespace to the compile unit.
   cu->Namespaces->Add( cn );
   return cu;
}

int main()
{

   // 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." );
   Console::WriteLine( "" );

   // Compile the CodeCompileUnit retrieved from the GetCompileUnit() method.
   //CSharpCodeProvider ^ provider = gcnew Microsoft::CSharp::CSharpCodeProvider;
   CodeDomProvider ^ provider = CodeDomProvider::CreateProvider("CSharp");

   // Initialize a CompilerParameters with the options for compilation.
   array<String^>^assemblies = {"System.dll"};
   CompilerParameters^ options = gcnew 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: " );
   Console::WriteLine( "" );
   for ( int i = 0; i < results->Errors->Count; i++ )
      Console::WriteLine( results->Errors[ i ] );
}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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