LoopExpression 类

定义

表示无限循环。 可以使用“break”退出它。

public ref class LoopExpression sealed : System::Linq::Expressions::Expression
public sealed class LoopExpression : System.Linq.Expressions.Expression
type LoopExpression = class
    inherit Expression
Public NotInheritable Class LoopExpression
Inherits Expression
继承
LoopExpression

示例

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

// 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

属性

名称 说明
Body

获取 Expression 循环正文。

BreakLabel

LabelTarget获取循环正文用作 break 语句目标。

CanReduce

指示节点可以缩减为更简单的节点。 如果返回 true,则可以调用 Reduce()以生成化简形式。

(继承自 Expression)
ContinueLabel

LabelTarget获取循环正文用作 continue 语句目标的语句。

NodeType

返回此表达式的节点类型。 重写此方法时,扩展节点应返回 Extension

Type

获取表示 Expression 的表达式的静态类型。

方法

名称 说明
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, LabelTarget, Expression)

创建类似于此表达式的新表达式,但使用提供的子级。 如果所有子级都相同,它将返回此表达式。

VisitChildren(ExpressionVisitor)

减少节点,然后在减少的表达式上调用访问者委托。 如果节点不可减少,该方法将引发异常。

(继承自 Expression)

适用于