英語で読む

次の方法で共有


CompilerError クラス

定義

コンパイラのエラーまたは警告を表します。

public class CompilerError
[System.Serializable]
public class CompilerError
継承
CompilerError
派生
属性

次の例では、CodeDOM プログラム グラフをコンパイルし、プログラムによって CompilerError データにアクセスする方法の例を示します。

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 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

こちらもご覧ください