Expression.Switch メソッド

定義

SwitchExpression ステートメントを表すswitchを作成します。

オーバーロード

名前 説明
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がない限り、SwitchExpression オブジェクト内のすべてのvoid オブジェクトの型は同じである必要があります。

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がない限り、SwitchExpression オブジェクト内のすべてのvoid オブジェクトの型は同じである必要があります。

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

適用対象