SwitchExpression 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个控制表达式,该表达式通过将控制传递到 SwitchCase 来处理多重选择。
public ref class SwitchExpression sealed : System::Linq::Expressions::Expressionpublic sealed class SwitchExpression : System.Linq.Expressions.Expressiontype SwitchExpression = class
    inherit ExpressionPublic NotInheritable Class SwitchExpression
Inherits Expression- 继承
示例
下面的示例演示如何使用 方法创建表示具有默认事例的 switch 语句的 Switch 表达式。
// Add the following directive to the file:
// using System.Linq.Expressions;
// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(3);
// This expression represents a switch statement
// that has a default case.
SwitchExpression switchExpr =
    Expression.Switch(
        switchValue,
        Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("Default")
                ),
        new SwitchCase[] {
            Expression.SwitchCase(
                Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("First")
                ),
                Expression.Constant(1)
            ),
            Expression.SwitchCase(
                Expression.Call(
                    null,
                    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                    Expression.Constant("Second")
                ),
                Expression.Constant(2)
            )
        }
    );
// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(switchExpr).Compile()();
// This code example produces the following output:
//
// Default
' Add the following directive to the file:
' Imports System.Linq.Expressions
' An expression that represents the switch value.
Dim switchValue As ConstantExpression = Expression.Constant(3)
' This expression represents a switch statement 
' that has a default case.
Dim switchExpr As SwitchExpression =
Expression.Switch(
    switchValue,
    Expression.Call(
                Nothing,
                GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
                Expression.Constant("Default")
            ),
    New SwitchCase() {
        Expression.SwitchCase(
            Expression.Call(
                Nothing,
                GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
                Expression.Constant("First")
            ),
            Expression.Constant(1)
        ),
        Expression.SwitchCase(
            Expression.Call(
                Nothing,
                GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
                Expression.Constant("Second")
            ),
            Expression.Constant(2)
        )
    }
)
' The following statement first creates an expression tree,
' then compiles it, and then runs it.
Expression.Lambda(Of Action)(switchExpr).Compile()()
' This code example produces the following output:
'
' Default
属性
| CanReduce | 指示可将节点简化为更简单的节点。 如果返回 true,则可以调用 Reduce() 以生成简化形式。(继承自 Expression) | 
| Cases | 获取开关的 SwitchCase 对象的集合。 | 
| Comparison | 获取相等比较方法(如果有)。 | 
| DefaultBody | 获取开关的测试。 | 
| NodeType | 返回此 Expression 的节点类型。 重写此方法时,扩展节点应返回 Extension。 | 
| SwitchValue | 获取开关的测试。 | 
| Type | 获取此 Expression 表示的表达式的静态类型。 | 
方法
| Accept(ExpressionVisitor) | 调度到此节点类型的特定 Visit 方法。 例如,MethodCallExpression 调用 VisitMethodCall(MethodCallExpression)。(继承自 Expression) | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Reduce() | 将此节点简化为更简单的表达式。 如果 CanReduce 返回 true,则它应返回有效的表达式。 此方法可以返回本身必须简化的另一个节点。(继承自 Expression) | 
| ReduceAndCheck() | 将此节点简化为更简单的表达式。 如果 CanReduce 返回 true,则它应返回有效的表达式。 此方法可以返回本身必须简化的另一个节点。(继承自 Expression) | 
| ReduceExtensions() | 将表达式简化为已知节点类型(即非 Extension 节点)或仅在此类型为已知类型时返回表达式。(继承自 Expression) | 
| ToString() | 返回 Expression 的的文本化表示形式。(继承自 Expression) | 
| Update(Expression, IEnumerable<SwitchCase>, Expression) | 创建一个新的表达式,它类似于此表达式,但使用所提供的子级。 如果所有子级均相同,它将返回此表达式。 | 
| VisitChildren(ExpressionVisitor) | 简化节点,然后对简化的表达式调用访问者委托。 该方法在节点不可简化时引发异常。(继承自 Expression) |