다음을 통해 공유


CompilerError 클래스

정의

컴파일러 오류 또는 경고를 나타냅니다.

public ref class CompilerError
public class CompilerError
[System.Serializable]
public class CompilerError
type CompilerError = class
[<System.Serializable>]
type CompilerError = class
Public Class CompilerError
상속
CompilerError
파생
특성

예제

다음 예제에서는 CodeDOM 프로그램 그래프를 컴파일하고 프로그래밍 방식으로 CompilerError 데이터에 액세스하는 방법의 예를 제공합니다.

#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 ] );
}
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

설명

CompilerError 는 컴파일러 오류 또는 컴파일러에서 반환된 경고를 나타냅니다.

참고

이 클래스는 모든 멤버에 적용 되는 클래스 수준에서 상속 요청을 포함 합니다. SecurityException 파생된 클래스에는 완전 신뢰 권한이 없는 경우 throw 됩니다. 상속 요구 사항에 대한 자세한 내용은 상속 요구를 참조하세요.

생성자

CompilerError()

CompilerError 클래스의 새 인스턴스를 초기화합니다.

CompilerError(String, Int32, Int32, String, String)

지정된 파일 이름, 줄, 열, 오류 번호 및 오류 텍스트를 사용하여 CompilerError 클래스의 새 인스턴스를 초기화합니다.

속성

Column

오류가 발생된 소스의 열 번호를 가져오거나 설정합니다.

ErrorNumber

오류 번호를 가져오거나 설정합니다.

ErrorText

오류 메시지의 텍스트를 가져오거나 설정합니다.

FileName

오류가 발생한 코드가 들어 있는 소스 파일의 파일 이름을 가져오거나 설정합니다.

IsWarning

오류가 경고인지 여부를 나타내는 값을 가져오거나 설정합니다.

Line

오류가 발생한 소스의 줄 번호를 가져오거나 설정합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

개체의 ToString() 메서드 구현을 제공합니다.

적용 대상

추가 정보