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, представляющий оператор 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

Тип результата оператора switch.

switchValue
Expression

Значение, проверяемое для каждого варианта case.

defaultBody
Expression

Результат оператора switch, если switchValue не соответствует ни одному из вариантов case.

comparison
MethodInfo

Используемый метод проверки равенства.

cases
SwitchCase[]

Набор вариантов case для данного выражения switch.

Возвращаемое значение

Созданный SwitchExpression.

Применяется к

Switch(Expression, SwitchCase[])

Исходный код:
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

Значение, проверяемое для каждого варианта case.

cases
SwitchCase[]

Набор вариантов case для данного выражения switch.

Возвращаемое значение

Созданный 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, представляющий оператор 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

Значение, проверяемое для каждого варианта case.

defaultBody
Expression

Результат оператора switch, если switchValue не соответствует ни одному из вариантов case.

cases
SwitchCase[]

Набор вариантов case для данного выражения switch.

Возвращаемое значение

Созданный 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, представляющий оператор 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

Значение, проверяемое для каждого варианта case.

defaultBody
Expression

Результат оператора switch, если switchValue не соответствует ни одному из вариантов case.

comparison
MethodInfo

Используемый метод проверки равенства.

cases
IEnumerable<SwitchCase>

Набор вариантов case для данного выражения switch.

Возвращаемое значение

Созданный SwitchExpression.

Применяется к

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

Исходный код:
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

Значение, проверяемое для каждого варианта case.

defaultBody
Expression

Результат оператора switch, если switchValue не соответствует ни одному из вариантов case.

comparison
MethodInfo

Используемый метод проверки равенства.

cases
SwitchCase[]

Набор вариантов case для данного выражения switch.

Возвращаемое значение

Созданный SwitchExpression.

Применяется к

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

Исходный код:
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

Тип результата оператора switch.

switchValue
Expression

Значение, проверяемое для каждого варианта case.

defaultBody
Expression

Результат оператора switch, если switchValue не соответствует ни одному из вариантов case.

comparison
MethodInfo

Используемый метод проверки равенства.

cases
IEnumerable<SwitchCase>

Набор вариантов case для данного выражения switch.

Возвращаемое значение

Созданный SwitchExpression.

Применяется к