語言

Expression.Switch 方法

定義

創造一個 SwitchExpression 代表陳述 switch 的 。

多載

名稱 Description
Switch(Type, Expression, Expression, MethodInfo, SwitchCase[])

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

Switch(Expression, SwitchCase[])

建立 SwitchExpression 一個表示 switch 沒有預設格的陳述。

Switch(Expression, Expression, SwitchCase[])

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

Switch(Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

Switch(Expression, Expression, MethodInfo, SwitchCase[])

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

Switch(Type, Expression, Expression, MethodInfo, SwitchCase[])

來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

public:
 static System::Linq::Expressions::SwitchExpression ^ Switch(Type ^ type, System::Linq::Expressions::Expression ^ switchValue, System::Linq::Expressions::Expression ^ defaultBody, System::Reflection::MethodInfo ^ comparison, ... cli::array <System::Linq::Expressions::SwitchCase ^> ^ cases);
public static System.Linq.Expressions.SwitchExpression Switch(Type type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, params System.Linq.Expressions.SwitchCase[] cases);
public static System.Linq.Expressions.SwitchExpression Switch(Type? type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, params System.Linq.Expressions.SwitchCase[]? cases);
static member Switch : Type * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Reflection.MethodInfo * System.Linq.Expressions.SwitchCase[] -> System.Linq.Expressions.SwitchExpression
Public Shared Function Switch (type As Type, switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, ParamArray cases As SwitchCase()) As SwitchExpression

參數

type
Type

切換的結果類型。

switchValue
Expression

要針對每個案例進行檢驗的價值。

defaultBody
Expression

切換 switchValue 結果與任何案例都不符。

comparison
MethodInfo

應採用的平等比較方法。

cases
SwitchCase[]

此切換式的情境集合。

傳回

被創造 SwitchExpression的。

適用於

Switch(Expression, SwitchCase[])

來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs

建立 SwitchExpression 一個表示 switch 沒有預設格的陳述。

public:
 static System::Linq::Expressions::SwitchExpression ^ Switch(System::Linq::Expressions::Expression ^ switchValue, ... cli::array <System::Linq::Expressions::SwitchCase ^> ^ cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, params System.Linq.Expressions.SwitchCase[] cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, params System.Linq.Expressions.SwitchCase[]? cases);
static member Switch : System.Linq.Expressions.Expression * System.Linq.Expressions.SwitchCase[] -> System.Linq.Expressions.SwitchExpression
Public Shared Function Switch (switchValue As Expression, ParamArray cases As SwitchCase()) As SwitchExpression

參數

switchValue
Expression

要針對每個案例進行檢驗的價值。

cases
SwitchCase[]

此切換式的情境集合。

傳回

被創造 SwitchExpression的。

範例

以下範例示範如何建立一個表示式,表示沒有預設案例的 switch 語句。

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

備註

物件中SwitchCase的所有SwitchExpression物件必須具有相同的類型,除非 具有 SwitchExpressionvoid類型 。

每個 SwitchCase 物件都有一個隱 break 含陳述,意即不會有從一個案例標籤到另一個的隱含落差。

若 switchValue 不符合任何情況,則不提出例外。

適用於

Switch(Expression, Expression, SwitchCase[])

來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

public:
 static System::Linq::Expressions::SwitchExpression ^ Switch(System::Linq::Expressions::Expression ^ switchValue, System::Linq::Expressions::Expression ^ defaultBody, ... cli::array <System::Linq::Expressions::SwitchCase ^> ^ cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, params System.Linq.Expressions.SwitchCase[] cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, params System.Linq.Expressions.SwitchCase[]? cases);
static member Switch : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.SwitchCase[] -> System.Linq.Expressions.SwitchExpression
Public Shared Function Switch (switchValue As Expression, defaultBody As Expression, ParamArray cases As SwitchCase()) As SwitchExpression

參數

switchValue
Expression

要針對每個案例進行檢驗的價值。

defaultBody
Expression

切換 switchValue 結果與任何案例都不符。

cases
SwitchCase[]

此切換式的情境集合。

傳回

被創造 SwitchExpression的。

範例

以下範例示範如何建立一個表示式,表示具有預設案例的 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

備註

物件中SwitchCase的所有SwitchExpression物件必須具有相同的類型,除非 具有 SwitchExpressionvoid類型 。

每個 SwitchCase 物件都有一個隱 break 含陳述,意即不會有從一個案例標籤到另一個的隱含落差。

若 switchValue 與任何情況不符,則執行 所 defaultBody 代表的預設案例。

適用於

Switch(Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

public:
 static System::Linq::Expressions::SwitchExpression ^ Switch(System::Linq::Expressions::Expression ^ switchValue, System::Linq::Expressions::Expression ^ defaultBody, System::Reflection::MethodInfo ^ comparison, System::Collections::Generic::IEnumerable<System::Linq::Expressions::SwitchCase ^> ^ cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase> cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase>? cases);
static member Switch : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Reflection.MethodInfo * seq<System.Linq.Expressions.SwitchCase> -> System.Linq.Expressions.SwitchExpression
Public Shared Function Switch (switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, cases As IEnumerable(Of SwitchCase)) As SwitchExpression

參數

switchValue
Expression

要針對每個案例進行檢驗的價值。

defaultBody
Expression

切換 switchValue 結果與任何案例都不符。

comparison
MethodInfo

應採用的平等比較方法。

cases
IEnumerable<SwitchCase>

此切換式的情境集合。

傳回

被創造 SwitchExpression的。

適用於

Switch(Expression, Expression, MethodInfo, SwitchCase[])

來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

public:
 static System::Linq::Expressions::SwitchExpression ^ Switch(System::Linq::Expressions::Expression ^ switchValue, System::Linq::Expressions::Expression ^ defaultBody, System::Reflection::MethodInfo ^ comparison, ... cli::array <System::Linq::Expressions::SwitchCase ^> ^ cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, params System.Linq.Expressions.SwitchCase[] cases);
public static System.Linq.Expressions.SwitchExpression Switch(System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, params System.Linq.Expressions.SwitchCase[]? cases);
static member Switch : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Reflection.MethodInfo * System.Linq.Expressions.SwitchCase[] -> System.Linq.Expressions.SwitchExpression
Public Shared Function Switch (switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, ParamArray cases As SwitchCase()) As SwitchExpression

參數

switchValue
Expression

要針對每個案例進行檢驗的價值。

defaultBody
Expression

切換 switchValue 結果與任何案例都不符。

comparison
MethodInfo

應採用的平等比較方法。

cases
SwitchCase[]

此切換式的情境集合。

傳回

被創造 SwitchExpression的。

適用於

Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)

來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs
來源:
SwitchExpression.cs

建立 SwitchExpression 一個代表 switch 有預設格的陳述。

public:
 static System::Linq::Expressions::SwitchExpression ^ Switch(Type ^ type, System::Linq::Expressions::Expression ^ switchValue, System::Linq::Expressions::Expression ^ defaultBody, System::Reflection::MethodInfo ^ comparison, System::Collections::Generic::IEnumerable<System::Linq::Expressions::SwitchCase ^> ^ cases);
public static System.Linq.Expressions.SwitchExpression Switch(Type type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression defaultBody, System.Reflection.MethodInfo comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase> cases);
public static System.Linq.Expressions.SwitchExpression Switch(Type? type, System.Linq.Expressions.Expression switchValue, System.Linq.Expressions.Expression? defaultBody, System.Reflection.MethodInfo? comparison, System.Collections.Generic.IEnumerable<System.Linq.Expressions.SwitchCase>? cases);
static member Switch : Type * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Reflection.MethodInfo * seq<System.Linq.Expressions.SwitchCase> -> System.Linq.Expressions.SwitchExpression
Public Shared Function Switch (type As Type, switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, cases As IEnumerable(Of SwitchCase)) As SwitchExpression

參數

type
Type

切換的結果類型。

switchValue
Expression

要針對每個案例進行檢驗的價值。

defaultBody
Expression

切換 switchValue 結果與任何案例都不符。

comparison
MethodInfo

應採用的平等比較方法。

cases
IEnumerable<SwitchCase>

此切換式的情境集合。

傳回

被創造 SwitchExpression的。

適用於