Expression.Break 方法

定义

创建一个表示 break 语句的 GotoExpression

重载

Break(LabelTarget)

创建一个表示 break 语句的 GotoExpression

Break(LabelTarget, Expression)

创建一个表示 break 语句的 GotoExpression。 可以指定在跳转时传递给标签的值。

Break(LabelTarget, Type)

创建一个表示具有指定类型的 break 语句的 GotoExpression

Break(LabelTarget, Expression, Type)

创建一个表示具有指定类型的 break 语句的 GotoExpression。 可以指定在跳转时传递给标签的值。

Break(LabelTarget)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

创建一个表示 break 语句的 GotoExpression

public:
 static System::Linq::Expressions::GotoExpression ^ Break(System::Linq::Expressions::LabelTarget ^ target);
public static System.Linq.Expressions.GotoExpression Break (System.Linq.Expressions.LabelTarget target);
static member Break : System.Linq.Expressions.LabelTarget -> System.Linq.Expressions.GotoExpression
Public Shared Function Break (target As LabelTarget) As GotoExpression

参数

target
LabelTarget

LabelTarget 将跳至的 GotoExpression

返回

一个 GotoExpression,其 Kind 等于 Break,其 Target 属性设置为 target,此外还有一个在跳转时将传递给目标标签的 null 值。

示例

下面的示例演示如何创建包含 LoopExpression 使用 Break 方法的 对象的表达式。

// Add the following directive to the file:
// using System.Linq.Expressions;

// Creating a parameter expression.
ParameterExpression value = Expression.Parameter(typeof(int), "value");

// Creating an expression to hold a local variable.
ParameterExpression result = Expression.Parameter(typeof(int), "result");

// Creating a label to jump to from a loop.
LabelTarget label = Expression.Label(typeof(int));

// Creating a method body.
BlockExpression block = Expression.Block(
    new[] { result },
    Expression.Assign(result, Expression.Constant(1)),
        Expression.Loop(
           Expression.IfThenElse(
               Expression.GreaterThan(value, Expression.Constant(1)),
               Expression.MultiplyAssign(result,
                   Expression.PostDecrementAssign(value)),
               Expression.Break(label, result)
           ),
       label
    )
);

// Compile and run an expression tree.
int factorial = Expression.Lambda<Func<int, int>>(block, value).Compile()(5);

Console.WriteLine(factorial);

// This code example produces the following output:
//
// 120
' Add the following directive to the file:
' Imports System.Linq.Expressions  

' Creating a parameter expression.
Dim value As ParameterExpression =
    Expression.Parameter(GetType(Integer), "value")

' Creating an expression to hold a local variable. 
Dim result As ParameterExpression =
    Expression.Parameter(GetType(Integer), "result")

' Creating a label to jump to from a loop.
Dim label As LabelTarget = Expression.Label(GetType(Integer))

' Creating a method body.
Dim block As BlockExpression = Expression.Block(
    New ParameterExpression() {result},
    Expression.Assign(result, Expression.Constant(1)),
    Expression.Loop(
        Expression.IfThenElse(
            Expression.GreaterThan(value, Expression.Constant(1)),
            Expression.MultiplyAssign(result,
                Expression.PostDecrementAssign(value)),
            Expression.Break(label, result)
        ),
        label
    )
)

' Compile an expression tree and return a delegate.
Dim factorial As Integer =
    Expression.Lambda(Of Func(Of Integer, Integer))(block, value).Compile()(5)

Console.WriteLine(factorial)

' This code example produces the following output:
'
' 120

适用于

Break(LabelTarget, Expression)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

创建一个表示 break 语句的 GotoExpression。 可以指定在跳转时传递给标签的值。

public:
 static System::Linq::Expressions::GotoExpression ^ Break(System::Linq::Expressions::LabelTarget ^ target, System::Linq::Expressions::Expression ^ value);
public static System.Linq.Expressions.GotoExpression Break (System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression value);
public static System.Linq.Expressions.GotoExpression Break (System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression? value);
static member Break : System.Linq.Expressions.LabelTarget * System.Linq.Expressions.Expression -> System.Linq.Expressions.GotoExpression
Public Shared Function Break (target As LabelTarget, value As Expression) As GotoExpression

参数

target
LabelTarget

LabelTarget 将跳至的 GotoExpression

value
Expression

将在跳转时传递给关联标签的值。

返回

一个 GotoExpression,其 Kind 等于 Break,其 Target 属性设置为 target,此外还有将在跳转时传递给目标标签的 value

适用于

Break(LabelTarget, Type)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

创建一个表示具有指定类型的 break 语句的 GotoExpression

public:
 static System::Linq::Expressions::GotoExpression ^ Break(System::Linq::Expressions::LabelTarget ^ target, Type ^ type);
public static System.Linq.Expressions.GotoExpression Break (System.Linq.Expressions.LabelTarget target, Type type);
static member Break : System.Linq.Expressions.LabelTarget * Type -> System.Linq.Expressions.GotoExpression
Public Shared Function Break (target As LabelTarget, type As Type) As GotoExpression

参数

target
LabelTarget

LabelTarget 将跳至的 GotoExpression

type
Type

要将 Type 属性设置为与其相等的 Type

返回

一个 GotoExpression,其 Kind 等于 Break,其 Target 属性设置为 target,并且其 Type 属性设置为 type

适用于

Break(LabelTarget, Expression, Type)

Source:
GotoExpression.cs
Source:
GotoExpression.cs
Source:
GotoExpression.cs

创建一个表示具有指定类型的 break 语句的 GotoExpression。 可以指定在跳转时传递给标签的值。

public:
 static System::Linq::Expressions::GotoExpression ^ Break(System::Linq::Expressions::LabelTarget ^ target, System::Linq::Expressions::Expression ^ value, Type ^ type);
public static System.Linq.Expressions.GotoExpression Break (System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression value, Type type);
public static System.Linq.Expressions.GotoExpression Break (System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression? value, Type type);
static member Break : System.Linq.Expressions.LabelTarget * System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.GotoExpression
Public Shared Function Break (target As LabelTarget, value As Expression, type As Type) As GotoExpression

参数

target
LabelTarget

LabelTarget 将跳至的 GotoExpression

value
Expression

将在跳转时传递给关联标签的值。

type
Type

要将 Type 属性设置为与其相等的 Type

返回

一个 GotoExpression,其 Kind 等于 Break,其 Target 属性设置为 target,并且其 Type 属性设置为 type,此外还有一个在跳转时将传递给目标标签的 value

适用于