CompilerError Class
Represents a compiler error or warning.
For a list of all members of this type, see CompilerError Members.
System.Object
System.CodeDom.Compiler.CompilerError
[Visual Basic] Public Class CompilerError [C#] public class CompilerError [C++] public __gc class CompilerError [JScript] public class CompilerError
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
CompilerError represents a compiler error or a warning that has been returned by the compiler.
Example
[Visual Basic, C#, C++] The following example compiles a CodeDOM program graph and provides an example of how to programmatically access CompilerError data.
[Visual Basic] Imports System Imports System.CodeDom Imports System.CodeDom.Compiler Imports Microsoft.CSharp Namespace CompilerError_Example _ Public Class Class1 Public Shared Sub 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 programatically.") Console.WriteLine("") ' Compile the CodeCompileUnit retrieved from the GetCompileUnit() method. Dim provider = New Microsoft.CSharp.CSharpCodeProvider() ' Create a CSharpCodeCompiler. Dim compiler As ICodeCompiler = provider.CreateCompiler() ' Initialize a CompilerParameters with the options for compilation. Dim assemblies() As String = New [String]() {"System.dll"} Dim options As New CompilerParameters(assemblies, "output.exe") ' Compile the CodeDOM graph and store the results in a CompilerResults. Dim results As CompilerResults = compiler.CompileAssemblyFromDom(options, GetCompileUnit()) ' Compilation produces errors. Print out each error. Console.WriteLine("Listing errors from compilation: ") Console.WriteLine("") Dim i As Integer For i = 0 To results.Errors.Count - 1 Console.WriteLine(results.Errors(i).ToString()) Next i End Sub Public Shared Function GetCompileUnit() As CodeCompileUnit ' Create a compile unit to contain a CodeDOM graph. Dim cu As New CodeCompileUnit() ' Create a namespace named TestSpace. Dim cn As New CodeNamespace("TestSpace") ' Declare a new type named TestClass. Dim cd As New CodeTypeDeclaration("TestClass") ' Declare a new member string field named TestField. Dim cmf As New CodeMemberField("System.String", "TestField") ' Add the field to the type. cd.Members.Add(cmf) ' Declare a new member method named TestMethod. Dim cm As New CodeMemberMethod() cm.Name = "TestMethod" ' Declare a string variable named TestVariable. Dim cvd As New CodeVariableDeclarationStatement("System.String1", "TestVariable") cm.Statements.Add(cvd) ' Cast the TestField reference expression to string and assign it to the TestVariable. Dim ca As 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)); 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 End Function End Class End Namespace [C#] using System; using System.CodeDom; using System.CodeDom.Compiler; using Microsoft.CSharp; namespace CompilerError_Example { public class Class1 { [STAThread] 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."); Console.WriteLine(""); // Compile the CodeCompileUnit retrieved from the GetCompileUnit() method. CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); // Create a CSharpCodeCompiler. ICodeCompiler compiler = provider.CreateCompiler(); // 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 = compiler.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].ToString()); } 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. cd.Members.Add(cmf); // 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"); cm.Statements.Add(cvd); // 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)); 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; } } } [C++] #using <mscorlib.dll> #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 = new CodeCompileUnit(); // Create a namespace named TestSpace. CodeNamespace* cn = new CodeNamespace(S"TestSpace"); // Declare a new type named TestClass. CodeTypeDeclaration* cd = new CodeTypeDeclaration(S"TestClass"); // Declare a new member string field named TestField. CodeMemberField* cmf = new CodeMemberField(S"System.String", S"TestField"); // Add the field to the type. cd->Members->Add(cmf); // Declare a new member method named TestMethod. CodeMemberMethod* cm = new CodeMemberMethod(); cm->Name = S"TestMethod"; // Declare a string variable named TestVariable. CodeVariableDeclarationStatement* cvd = new CodeVariableDeclarationStatement(S"System.String1", S"TestVariable"); cm->Statements->Add(cvd); // Cast the TestField reference expression to string and assign it to the TestVariable. CodeAssignStatement* ca = new CodeAssignStatement(new CodeVariableReferenceExpression(S"TestVariable"), new CodeCastExpression(S"System.String2", new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), S"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(S"This program compiles a CodeDOM program that incorrectly declares multiple data"); Console::WriteLine(S"types to demonstrate handling compiler errors programmatically."); Console::WriteLine(S""); // Compile the CodeCompileUnit retrieved from the GetCompileUnit() method. CSharpCodeProvider* provider = new Microsoft::CSharp::CSharpCodeProvider(); // Create a CSharpCodeCompiler. ICodeCompiler* compiler = provider->CreateCompiler(); // Initialize a CompilerParameters with the options for compilation. String* assemblies[] = {S"System.dll"}; CompilerParameters* options = new CompilerParameters( assemblies, S"output.exe"); // Compile the CodeDOM graph and store the results in a CompilerResults. CompilerResults* results = compiler->CompileAssemblyFromDom(options, GetCompileUnit()); // Compilation produces errors. Print out each error. Console::WriteLine(S"Listing errors from compilation: "); Console::WriteLine(S""); for( int i=0; i<results->Errors->Count; i++) Console::WriteLine(results->Errors->Item[i]); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.CodeDom.Compiler
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System (in System.dll)
See Also
CompilerError Members | System.CodeDom.Compiler Namespace | CompilerErrorCollection