CodeLabeledStatement 類別

定義

表示標記陳述式 (Label Statement) 或是獨立標記。Represents a labeled statement or a stand-alone label.

public ref class CodeLabeledStatement : System::CodeDom::CodeStatement
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeLabeledStatement : System.CodeDom.CodeStatement
type CodeLabeledStatement = class
    inherit CodeStatement
Public Class CodeLabeledStatement
Inherits CodeStatement
繼承
CodeLabeledStatement
屬性

範例

下列範例程式碼示範如何使用CodeGotoStatement CodeLabeledStatement和來重新導向程式流程。The following example code demonstrates use of a CodeGotoStatement and a CodeLabeledStatement to redirect program flow.

// Declares a type to contain the example code.
CodeTypeDeclaration^ type1 = gcnew CodeTypeDeclaration( "Type1" );

// Declares an entry point method.
CodeEntryPointMethod^ entry1 = gcnew CodeEntryPointMethod;
type1->Members->Add( entry1 );

// Adds a goto statement to continue program flow at the "JumpToLabel" label.
CodeGotoStatement^ goto1 = gcnew CodeGotoStatement( "JumpToLabel" );
entry1->Statements->Add( goto1 );

// Invokes Console.WriteLine to print "Test Output", which is skipped by the goto statement.
array<CodeExpression^>^temp = {gcnew CodePrimitiveExpression( "Test Output." )};
CodeMethodInvokeExpression^ method1 = gcnew CodeMethodInvokeExpression( gcnew CodeTypeReferenceExpression( "System.Console" ),"WriteLine",temp );
entry1->Statements->Add( method1 );

// Declares a label named "JumpToLabel" associated with a method to output a test string using Console.WriteLine.
array<CodeExpression^>^temp2 = {gcnew CodePrimitiveExpression( "Output from labeled statement." )};
CodeMethodInvokeExpression^ method2 = gcnew CodeMethodInvokeExpression( gcnew CodeTypeReferenceExpression( "System.Console" ),"WriteLine",temp2 );
CodeLabeledStatement^ label1 = gcnew CodeLabeledStatement( "JumpToLabel",gcnew 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.");
//        }
//    }
// 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.");
//        }
//    }
' Declares a type to contain the example code.
Dim type1 As New CodeTypeDeclaration("Type1")
' Declares an entry point method.
Dim entry1 As New CodeEntryPointMethod()
type1.Members.Add(entry1)
' Adds a goto statement to continue program flow at the "JumpToLabel" label.
Dim goto1 As New CodeGotoStatement("JumpToLabel")
entry1.Statements.Add(goto1)
' Invokes Console.WriteLine to print "Test Output", which is skipped by the goto statement.
Dim method1 As 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.
Dim method2 As New CodeMethodInvokeExpression(New CodeTypeReferenceExpression("System.Console"), "WriteLine", New CodePrimitiveExpression("Output from labeled statement."))
Dim label1 As New CodeLabeledStatement("JumpToLabel", New CodeExpressionStatement(method2))
entry1.Statements.Add(label1)

' A Visual Basic code generator produces the following source code for the preceeding example code:

'   Public Class Type1
'
'       Public Shared Sub Main()
'           GoTo JumpToLabel
'           System.Console.WriteLine("Test Output")
'           JumpToLabel:
'           System.Console.WriteLine("Output from labeled statement.")
'       End Sub
'   End Class

備註

CodeLabeledStatement表示標籤以及 (選擇性) 相關聯的語句。CodeLabeledStatement represents a label and optionally, an associated statement. 標籤可以用來表示的目標CodeGotoStatementA label can be used to indicate the target of a CodeGotoStatement.

屬性Statement是選擇性的。The Statement property is optional. 若只要建立標籤, 請讓Statement屬性保持未初始化。To create only a label, leave the Statement property uninitialized.

注意

並非所有語言都goto支援語句和標籤, 因此您應該藉由使用GotoStatementsgoto標來呼叫Supports方法, 測試程式碼產生器是否支援語句和標籤。Not all languages support goto statements and labels, so you should test whether a code generator supports goto statements and labels by calling the Supports method with the GotoStatements flag.

建構函式

CodeLabeledStatement()

初始化 CodeLabeledStatement 類別的新執行個體。Initializes a new instance of the CodeLabeledStatement class.

CodeLabeledStatement(String)

使用指定的標記名稱,初始化 CodeLabeledStatement 類別的新執行個體。Initializes a new instance of the CodeLabeledStatement class using the specified label name.

CodeLabeledStatement(String, CodeStatement)

使用指定的標記和陳述式,初始化 CodeLabeledStatement 類別的新執行個體。Initializes a new instance of the CodeLabeledStatement class using the specified label name and statement.

屬性

EndDirectives

取得包含結尾指示詞的 CodeDirectiveCollection 物件。Gets a CodeDirectiveCollection object that contains end directives.

(繼承來源 CodeStatement)
Label

取得或設定標記名稱。Gets or sets the name of the label.

LinePragma

取得或設定程式碼陳述式發生所在的行。Gets or sets the line on which the code statement occurs.

(繼承來源 CodeStatement)
StartDirectives

取得包含開頭指示詞的 CodeDirectiveCollection 物件。Gets a CodeDirectiveCollection object that contains start directives.

(繼承來源 CodeStatement)
Statement

取得或設定選擇性相關陳述式。Gets or sets the optional associated statement.

UserData

取得目前物件的使用者可定義資料。Gets the user-definable data for the current object.

(繼承來源 CodeObject)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱