閱讀英文版本

分享方式:


CodeLabeledStatement 類別

定義

表示標記陳述式 (Label Statement) 或是獨立標記。

C#
public class CodeLabeledStatement : System.CodeDom.CodeStatement
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeLabeledStatement : System.CodeDom.CodeStatement
繼承
CodeLabeledStatement
屬性

範例

下列範例程式代碼示範如何使用 CodeGotoStatementCodeLabeledStatement 來重新導向程式流程。

C#
// Declares a type to contain the example code.
CodeTypeDeclaration type1 = new CodeTypeDeclaration("Type1");
// Declares an entry point method.
CodeEntryPointMethod entry1 = new CodeEntryPointMethod();
type1.Members.Add( entry1 );
// Adds a goto statement to continue program flow at the "JumpToLabel" label.
CodeGotoStatement goto1 = new CodeGotoStatement("JumpToLabel");
entry1.Statements.Add( goto1 );
// Invokes Console.WriteLine to print "Test Output", which is skipped by the goto statement.
CodeMethodInvokeExpression method1 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression("System.Console"), "WriteLine", new CodePrimitiveExpression("Test Output."));
entry1.Statements.Add( method1 );
// Declares a label named "JumpToLabel" associated with a method to output a test string using Console.WriteLine.
CodeMethodInvokeExpression method2 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression("System.Console"), "WriteLine", new CodePrimitiveExpression("Output from labeled statement."));
CodeLabeledStatement label1 = new CodeLabeledStatement("JumpToLabel", new CodeExpressionStatement(method2) );
entry1.Statements.Add( label1 );

// A C# code generator produces the following source code for the preceeding example code:

//    public class Type1
//    {
//
//        public static void Main()
//        {
//            goto JumpToLabel;
//            System.Console.WriteLine("Test Output");
//            JumpToLabel:
//            System.Console.WriteLine("Output from labeled statement.");
//        }
//    }

備註

CodeLabeledStatement 表示標籤,並選擇性地表示相關聯的語句。 標籤可用來表示 的目標 CodeGotoStatement

Statement 是選用屬性。 若要只建立標籤,請將 屬性保留 Statement 為未初始化。

備註

並非所有語言都支援goto語句和標籤,因此您應該使用 旗標呼叫 Supports 方法來GotoStatements測試程式代碼產生器是否支援 goto 語句和標籤。

建構函式

CodeLabeledStatement()

初始化 CodeLabeledStatement 類別的新執行個體。

CodeLabeledStatement(String)

使用指定的標記名稱,初始化 CodeLabeledStatement 類別的新執行個體。

CodeLabeledStatement(String, CodeStatement)

使用指定的標記和陳述式,初始化 CodeLabeledStatement 類別的新執行個體。

屬性

EndDirectives

取得包含結尾指示詞的 CodeDirectiveCollection 物件。

(繼承來源 CodeStatement)
Label

取得或設定標記名稱。

LinePragma

取得或設定程式碼陳述式發生所在的行。

(繼承來源 CodeStatement)
StartDirectives

取得包含開頭指示詞的 CodeDirectiveCollection 物件。

(繼承來源 CodeStatement)
Statement

取得或設定選擇性相關陳述式。

UserData

取得目前物件的使用者可定義資料。

(繼承來源 CodeObject)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

產品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另請參閱