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

範例

以下範例示範如何利用該LoopExpression方法建立包含Loop物件的區塊表達式。

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

屬性

名稱 Description
Body

明白 Expression 那是迴圈的主體。

BreakLabel

取得 LabelTarget that 被迴圈主體用作斷線陳述句的目標。

CanReduce

表示該節點可以簡化為更簡單的節點。 若此結果為真,則可呼叫 Reduce() 以產生約簡形式。

(繼承來源 Expression)
ContinueLabel

取得 LabelTarget that 被迴圈主體用作繼續陳述目標。

NodeType

回傳此表達式的節點類型。 當覆寫此方法時,擴充節點應該會回傳 Extension

Type

取得此 Expression 表達式的靜態型態。

方法

名稱 Description
Accept(ExpressionVisitor)

針對此節點類型的特定訪問方法進行派遣。 例如,稱為 MethodCallExpressionVisitMethodCall(MethodCallExpression)

(繼承來源 Expression)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Reduce()

將此節點簡化為更簡單的表達式。 如果 CanReduce 回傳為真,則應回傳一個有效的表達式。 此方法可返回另一個必須被約簡的節點。

(繼承來源 Expression)
ReduceAndCheck()

將此節點簡化為更簡單的表達式。 如果 CanReduce 回傳為真,則應回傳一個有效的表達式。 此方法可返回另一個必須被約簡的節點。

(繼承來源 Expression)
ReduceExtensions()

將表達式簡化為已知節點型別(非擴充節點),或僅回傳已是已知型別的表達式。

(繼承來源 Expression)
ToString()

回傳 的文字表示 Expression

(繼承來源 Expression)
Update(LabelTarget, LabelTarget, Expression)

會建立一個新的表達式,類似這個,但使用提供的子節點。 如果所有子節點相同,則會回傳這個表達式。

VisitChildren(ExpressionVisitor)

將節點簡化,然後呼叫訪客代理處理簡化表達式。 若節點不可約,方法會拋出例外。

(繼承來源 Expression)

適用於