SwitchCase 类

定义

表示 SwitchExpression 的一个事例。Represents one case of a SwitchExpression.

public ref class SwitchCase sealed
public sealed class SwitchCase
type SwitchCase = class
Public NotInheritable Class SwitchCase
继承
SwitchCase

示例

下面的示例演示如何使用方法创建一个表达式,该表达式表示无默认情况下的 switch 语句 SwitchCaseThe following example demonstrates how to create an expression that represents a switch statement without a default case by using the SwitchCase method.

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

// An expression that represents the switch value.
ConstantExpression switchValue = Expression.Constant(2);

// This expression represents a switch statement
// without a default case.
SwitchExpression switchExpr =
    Expression.Switch(
        switchValue,
        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:
//
// Second
' Add the following directive to the file:
' Imports System.Linq.Expressions

' An expression that represents the switch value.
Dim switchValue As ConstantExpression = Expression.Constant(2)

' This expression represents a switch statement 
' without a default case.
Dim switchExpr As SwitchExpression =
Expression.Switch(
    switchValue,
    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:
'
' Second

属性

Body

获取此事例的主体。Gets the body of this case.

TestValues

获取此事例的值。Gets the values of this case. SwitchValue 与其中任一值匹配时,会选择此事例以便执行。This case is selected for execution when the SwitchValue matches any of these values.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前 Object 的一个StringReturns a String that represents the current Object.

Update(IEnumerable<Expression>, Expression)

创建一个新的表达式,它类似于此表达式,但使用所提供的子级。Creates a new expression that is like this one, but using the supplied children. 如果所有子级均相同,它将返回此表达式。If all of the children are the same, it will return this expression.

适用于