CompilerError Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Rappresenta un errore o un avviso del compilatore.
public ref class CompilerError
public class CompilerError
[System.Serializable]
public class CompilerError
type CompilerError = class
[<System.Serializable>]
type CompilerError = class
Public Class CompilerError
- Ereditarietà
-
CompilerError
- Derivato
- Attributi
Esempio
Nell'esempio seguente viene compilato un grafo del programma CodeDOM e viene fornito un esempio di come accedere ai dati CompilerError a livello di codice.
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();
// 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: ");
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;
}
}
}
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp
Namespace CompilerError_Example
_
Class Class1
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 As CodeDomProvider
provider = CodeDomProvider.CreateProvider("CSharp")
' 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 = provider.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
Commenti
CompilerError rappresenta un errore del compilatore o un avviso restituito dal compilatore.
Annotazioni
Questa classe contiene una richiesta di ereditarietà a livello di classe che si applica a tutti i membri. Viene SecurityException generata un'eccezione quando la classe derivata non dispone dell'autorizzazione di attendibilità completa.
Costruttori
| Nome | Descrizione |
|---|---|
| CompilerError() |
Inizializza una nuova istanza della classe CompilerError. |
| CompilerError(String, Int32, Int32, String, String) |
Inizializza una nuova istanza della CompilerError classe utilizzando il nome file, la riga, la colonna, il numero di errore e il testo di errore specificati. |
Proprietà
| Nome | Descrizione |
|---|---|
| Column |
Ottiene o imposta il numero di colonna in cui si verifica l'origine dell'errore. |
| ErrorNumber |
Ottiene o imposta il numero di errore. |
| ErrorText |
Ottiene o imposta il testo del messaggio di errore. |
| FileName |
Ottiene o imposta il nome file del file di origine che contiene il codice che ha causato l'errore. |
| IsWarning |
Ottiene o imposta un valore che indica se l'errore è un avviso. |
| Line |
Ottiene o imposta il numero di riga in cui si verifica l'origine dell'errore. |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
| GetHashCode() |
Funge da funzione hash predefinita. (Ereditato da Object) |
| GetType() |
Ottiene il Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Fornisce un'implementazione del metodo object ToString() . |