ConditionalExpression 类

定义

表示具有条件运算符的表达式。

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
继承
ConditionalExpression

示例

下面的代码示例演示如何创建表示条件语句的表达式。 如果第一个参数的计算结果为 true,则执行第二个参数;否则,将执行第三个参数。

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

注解

使用 Condition 工厂方法创建一个 ConditionalExpression

NodeTypeConditionalExpressionConditional

属性

名称 说明
CanReduce

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

(继承自 Expression)
IfFalse

获取要执行的表达式(如果测试的计算结果为 false.

IfTrue

获取要执行的表达式(如果测试的计算结果为 true.

NodeType

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

NodeType

获取此 Expression类型的节点类型。

(继承自 Expression)
Test

获取条件操作的测试。

Type

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

Type

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

(继承自 Expression)

方法

名称 说明
Accept(ExpressionVisitor)

调度到此节点类型的特定访问方法。 例如,MethodCallExpression调用 .VisitMethodCall(MethodCallExpression)

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(Expression, Expression, Expression)

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

VisitChildren(ExpressionVisitor)

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

(继承自 Expression)

适用于