Expression.Switch Метод

Определение

Создает оператор SwitchExpression , представляющий инструкцию switch .

Перегрузки

Имя Описание
Switch(Type, Expression, Expression, MethodInfo, SwitchCase[])

Создает инструкцию SwitchExpression , которая имеет switch регистр по умолчанию.

Switch(Expression, SwitchCase[])

Создает инструкцию SwitchExpressionswitch без регистра по умолчанию.

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

Создает инструкцию SwitchExpressionswitch без регистра по умолчанию.

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объект.

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