CompilerError 類別

定義

表示編譯器錯誤或警告。

C#
public class CompilerError
C#
[System.Serializable]
public class CompilerError
繼承
CompilerError
衍生
屬性

範例

下列範例會編譯 CodeDOM 程式圖形,並提供如何以程式設計方式存取 CompilerError 數據的範例。

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();

            // 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;
        }
    }
}

備註

CompilerError 表示編譯程式錯誤或編譯程式傳回的警告。

備註

這個類別包含適用於所有成員之類別層級的繼承需求。 SecurityException當衍生類別沒有完全信任權限時,會擲回 。 如需繼承需求的詳細資訊,請參閱 繼承需求

建構函式

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() 方法的實作。

適用於

產品 版本
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另請參閱