GotoExpression 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示无条件跳跃。 这包括 return 语句、break 和 continue 语句和其他跳转。
public ref class GotoExpression sealed : System::Linq::Expressions::Expression
public sealed class GotoExpression : System.Linq.Expressions.Expression
type GotoExpression = class
inherit Expression
Public NotInheritable Class GotoExpression
Inherits Expression
- 继承
示例
以下示例演示如何使用Goto该方法创建包含GotoExpression对象的表达式。
// Add the following directive to your file:
// using System.Linq.Expressions;
// A label expression of the void type that is the target for the GotoExpression.
LabelTarget returnTarget = Expression.Label();
// This block contains a GotoExpression.
// It transfers execution to a label expression that is initialized with the same LabelTarget as the GotoExpression.
// The types of the GotoExpression, label expression, and LabelTarget must match.
BlockExpression blockExpr =
Expression.Block(
Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("GoTo")),
Expression.Goto(returnTarget),
Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("Other Work")),
Expression.Label(returnTarget)
);
// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(blockExpr).Compile()();
// This code example produces the following output:
//
// GoTo
// "Other Work" is not printed because
// the GoTo expression transfers execution from Expression.GoTo(returnTarget)
// to Expression.Label(returnTarget).
' Add the following directive to your file:
' Imports System.Linq.Expressions
' A label expression of the void type that is the target for the GoToExpression.
Dim returnTarget As LabelTarget = Expression.Label()
' This block contains a GotoExpression.
' It transfers execution to a label expression that is initialized with the same LabelTarget as the GotoExpression.
' The types of the GotoExpression, label expression, and LabelTarget must match.
Dim blockExpr As BlockExpression =
Expression.Block(
Expression.Call(GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}), Expression.Constant("GoTo")),
Expression.Goto(returnTarget),
Expression.Call(GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}), Expression.Constant("Other Work")),
Expression.Label(returnTarget)
)
' The following statement first creates an expression tree,
' then compiles it, and then runs it.
Expression.Lambda(Of Action)(blockExpr).Compile()()
' This code example produces the following output:
'
' GoTo
' "Other Work" is not printed because
' the Return expression transfers execution from Expression.GoTo(returnTarget)
' to Expression.Label(returnTarget).
属性
| 名称 | 说明 |
|---|---|
| CanReduce |
指示节点可以缩减为更简单的节点。 如果返回 true,则可以调用 Reduce()以生成化简形式。 (继承自 Expression) |
| Kind |
“转到”表达式的类型。 仅用于信息目的。 |
| NodeType |
返回此 Expression类型的节点类型。 |
| Target |
此节点跳转到的目标标签。 |
| Type |
获取表示 Expression 的表达式的静态类型。 |
| Value |
传递给目标的值,如果目标的类型为 System.Void,则为 null。 |
方法
| 名称 | 说明 |
|---|---|
| Accept(ExpressionVisitor) |
调度到此节点类型的特定访问方法。 例如,MethodCallExpression调用 .VisitMethodCall(MethodCallExpression) (继承自 Expression) |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| Reduce() |
将此节点减少为更简单的表达式。 如果 CanReduce 返回 true,则应返回有效的表达式。 此方法可以返回必须自行减少的另一个节点。 (继承自 Expression) |
| ReduceAndCheck() |
将此节点减少为更简单的表达式。 如果 CanReduce 返回 true,则应返回有效的表达式。 此方法可以返回必须自行减少的另一个节点。 (继承自 Expression) |
| ReduceExtensions() |
将表达式减少为已知节点类型(不是扩展节点),或者仅返回表达式(如果它已是已知类型)。 (继承自 Expression) |
| ToString() |
返回文本 Expression表示形式。 (继承自 Expression) |
| Update(LabelTarget, Expression) |
创建类似于此表达式的新表达式,但使用提供的子级。 如果所有子级都相同,它将返回此表达式。 |
| VisitChildren(ExpressionVisitor) |
减少节点,然后在减少的表达式上调用访问者委托。 如果节点不可减少,该方法将引发异常。 (继承自 Expression) |