ConditionalExpression Klasa

Definicja

Reprezentuje wyrażenie, które ma operator warunkowy.

public ref class ConditionalExpression : System::Linq::Expressions::Expression
public ref class ConditionalExpression sealed : System::Linq::Expressions::Expression
public class ConditionalExpression : System.Linq.Expressions.Expression
public sealed class ConditionalExpression : System.Linq.Expressions.Expression
type ConditionalExpression = class
    inherit Expression
Public Class ConditionalExpression
Inherits Expression
Public NotInheritable Class ConditionalExpression
Inherits Expression
Dziedziczenie
ConditionalExpression

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć wyrażenie reprezentujące instrukcję warunkową. Jeśli pierwszy argument zwróci wartość true, drugi argument jest wykonywany. W przeciwnym razie jest wykonywany trzeci argument.

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

int num = 100;

// This expression represents a conditional operation.
// It evaluates the test (first expression) and
// executes the iftrue block (second argument) if the test evaluates to true,
// or the iffalse block (third argument) if the test evaluates to false.
Expression conditionExpr = Expression.Condition(
                           Expression.Constant(num > 10),
                           Expression.Constant("num is greater than 10"),
                           Expression.Constant("num is smaller than 10")
                         );

// Print out the expression.
Console.WriteLine(conditionExpr.ToString());

// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(
    Expression.Lambda<Func<string>>(conditionExpr).Compile()());

// This code example produces the following output:
//
// IIF("True", "num is greater than 10", "num is smaller than 10")
// num is greater than 10
' Add the following directive to your file:
' Imports System.Linq.Expressions  

Dim num As Integer = 100

' This expression represents a conditional operation; 
' it will evaluate the test (first expression) and
' execute the ifTrue block (second argument) if the test evaluates to true, 
' or the ifFalse block (third argument) if the test evaluates to false.
Dim conditionExpr As Expression = Expression.Condition(
                            Expression.Constant(num > 10),
                            Expression.Constant("n is greater than 10"),
                            Expression.Constant("n is smaller than 10")
                        )

' Print the expression.
Console.WriteLine(conditionExpr.ToString())

' The following statement first creates an expression tree,
' then compiles it, and then executes it.       
Console.WriteLine(
    Expression.Lambda(Of Func(Of String))(conditionExpr).Compile()())

' This code example produces the following output:
'
' IIF("True", "num is greater than 10", "num is smaller than 10")
' num is greater than 10

Uwagi

Condition Użyj metody factory, aby utworzyć element ConditionalExpression.

Wartość NodeType typu ConditionalExpression to Conditional.

Właściwości

CanReduce

Wskazuje, że węzeł można zmniejszyć do prostszego węzła. Jeśli zwraca wartość true, funkcja Reduce() może zostać wywołana w celu utworzenia skróconej postaci.

(Odziedziczone po Expression)
IfFalse

Pobiera wyrażenie do wykonania, jeśli test zwróci wartość false.

IfTrue

Pobiera wyrażenie do wykonania, jeśli test zwróci wartość true.

NodeType

Zwraca typ węzła tego wyrażenia. Węzły rozszerzeń powinny zwracać Extension podczas zastępowania tej metody.

NodeType

Pobiera typ węzła tego Expressionelementu .

(Odziedziczone po Expression)
Test

Pobiera test operacji warunkowej.

Type

Pobiera statyczny typ wyrażenia, które reprezentuje Expression .

Type

Pobiera statyczny typ wyrażenia, które reprezentuje Expression .

(Odziedziczone po Expression)

Metody

Accept(ExpressionVisitor)

Wysyła do określonej metody visit dla tego typu węzła. Na przykład MethodCallExpression wywołuje metodę VisitMethodCall(MethodCallExpression).

Accept(ExpressionVisitor)

Wysyła do określonej metody visit dla tego typu węzła. Na przykład MethodCallExpression wywołuje metodę VisitMethodCall(MethodCallExpression).

(Odziedziczone po Expression)
Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
Reduce()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinno to zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który należy zmniejszyć.

(Odziedziczone po Expression)
ReduceAndCheck()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinno to zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który należy zmniejszyć.

(Odziedziczone po Expression)
ReduceExtensions()

Zmniejsza wyrażenie do znanego typu węzła (który nie jest węzłem rozszerzenia) lub po prostu zwraca wyrażenie, jeśli jest to już znany typ.

(Odziedziczone po Expression)
ToString()

Zwraca tekstową reprezentację obiektu Expression.

(Odziedziczone po Expression)
Update(Expression, Expression, Expression)

Tworzy nowe wyrażenie, które jest podobne do tego, ale przy użyciu dostarczonych elementów podrzędnych. Jeśli wszystkie elementy podrzędne są takie same, zwróci to wyrażenie.

VisitChildren(ExpressionVisitor)

Zmniejsza węzeł, a następnie wywołuje delegata odwiedzających w wyrażeniu zredukowanym. Metoda zgłasza wyjątek, jeśli węzeł nie jest reducible.

(Odziedziczone po Expression)

Dotyczy