Expression.Condition 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建一个表示条件语句的 ConditionalExpression。
重载
Condition(Expression, Expression, Expression) |
创建一个表示条件语句的 ConditionalExpression。 |
Condition(Expression, Expression, Expression, Type) |
创建一个表示条件语句的 ConditionalExpression。 |
Condition(Expression, Expression, Expression)
- Source:
- ConditionalExpression.cs
- Source:
- ConditionalExpression.cs
- Source:
- ConditionalExpression.cs
创建一个表示条件语句的 ConditionalExpression。
public:
static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse);
public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse As Expression) As ConditionalExpression
参数
- test
- Expression
要将 Expression 属性设置为与其相等的 Test。
- ifTrue
- Expression
要将 Expression 属性设置为与其相等的 IfTrue。
- ifFalse
- Expression
要将 Expression 属性设置为与其相等的 IfFalse。
返回
一个 ConditionalExpression,其 NodeType 属性等于 Conditional,并且其 Test、IfTrue 和 IfFalse 属性设置为指定值。
例外
test
、ifTrue
或 ifFalse
为 null
。
示例
下面的代码示例演示如何创建表示条件语句的表达式。 如果第一个参数的计算结果为 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
注解
Type生成的 ConditionalExpression 的 属性等于 Type 的 ifTrue
属性。
另请参阅
适用于
Condition(Expression, Expression, Expression, Type)
- Source:
- ConditionalExpression.cs
- Source:
- ConditionalExpression.cs
- Source:
- ConditionalExpression.cs
创建一个表示条件语句的 ConditionalExpression。
public:
static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse, Type ^ type);
public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse, Type type);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse As Expression, type As Type) As ConditionalExpression
参数
- test
- Expression
要将 Expression 属性设置为与其相等的 Test。
- ifTrue
- Expression
要将 Expression 属性设置为与其相等的 IfTrue。
- ifFalse
- Expression
要将 Expression 属性设置为与其相等的 IfFalse。
返回
一个 ConditionalExpression,其 NodeType 属性等于 Conditional,并且其 Test、IfTrue 和 IfFalse 属性设置为指定值。
注解
此方法允许在 和 ifFalse
表达式类型不相等的情况下显式统一条件表达式的结果类型ifTrue
。 和 ifFalse
的类型ifTrue
必须是可分配给结果类型的隐式引用。 type
允许 为 Void。