CodeGotoStatement 类

定义

表示 goto 语句。Represents a goto statement.

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

示例

下面的示例代码演示如何使用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

注解

CodeGotoStatement可用于表示goto语句, 该语句在某些语言中用于将程序流重定向到指定标签。CodeGotoStatement can be used to represent a goto statement, which is used in some languages to redirect program flow to a specified label.

Label属性指示继续执行程序的标签名称。The Label property indicates the name of the label at which to continue program execution.

备注

并非所有语言都goto支持语句。Not all languages support goto statements. 使用标志调用方法,goto以确定代码生成器是否支持语句。 Supports GotoStatementsCall the Supports method with the GotoStatements flag to determine whether a code generator supports goto statements.

构造函数

CodeGotoStatement()

初始化 CodeGotoStatement 类的新实例。Initializes a new instance of the CodeGotoStatement class.

CodeGotoStatement(String)

使用指定的标签名初始化 CodeGotoStatement 类的新实例。Initializes a new instance of the CodeGotoStatement class using the specified label name.

属性

EndDirectives

获取包含结束指令的 CodeDirectiveCollection 对象。Gets a CodeDirectiveCollection object that contains end directives.

(继承自 CodeStatement)
Label

获取或设置程序继续执行处的标签名称。Gets or sets the name of the label at which to continue program execution.

LinePragma

获取或设置代码语句所在的行。Gets or sets the line on which the code statement occurs.

(继承自 CodeStatement)
StartDirectives

获取包含开始指令的 CodeDirectiveCollection 对象。Gets a CodeDirectiveCollection object that contains start directives.

(继承自 CodeStatement)
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 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅