Expression.Switch Método

Definición

Crea un SwitchExpression objeto que representa una switch instrucción .

Sobrecargas

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

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

Switch(Expression, SwitchCase[])

Crea un SwitchExpression objeto que representa una switch instrucción sin un caso predeterminado.

Switch(Expression, Expression, SwitchCase[])

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Parámetros

type
Type

Tipo de resultado del modificador.

switchValue
Expression

Valor que se va a probar en cada caso.

defaultBody
Expression

Resultado del modificador si switchValue no coincide con ninguno de los casos.

comparison
MethodInfo

Método de comparación de igualdad que se va a usar.

cases
SwitchCase[]

Conjunto de casos para esta expresión switch.

Devoluciones

Objeto creado SwitchExpression.

Se aplica a

Switch(Expression, SwitchCase[])

Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs

Crea un SwitchExpression objeto que representa una switch instrucción sin un caso predeterminado.

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

Parámetros

switchValue
Expression

Valor que se va a probar en cada caso.

cases
SwitchCase[]

Conjunto de casos para esta expresión switch.

Devoluciones

Objeto creado SwitchExpression.

Ejemplos

En el ejemplo siguiente se muestra cómo crear una expresión que represente una instrucción switch sin un caso predeterminado.

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

Comentarios

Todos los SwitchCase objetos de un SwitchExpression objeto deben tener el mismo tipo, a menos que SwitchExpression tenga el tipo void.

Cada SwitchCase objeto tiene una instrucción implícita break , lo que significa que no hay ninguna caída implícita de una etiqueta de mayúsculas y minúsculas a otra.

Si switchValue no coincide con ninguno de los casos, no se produce ninguna excepción.

Se aplica a

Switch(Expression, Expression, SwitchCase[])

Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Parámetros

switchValue
Expression

Valor que se va a probar en cada caso.

defaultBody
Expression

Resultado del modificador si switchValue no coincide con ninguno de los casos.

cases
SwitchCase[]

Conjunto de casos para esta expresión switch.

Devoluciones

Objeto creado SwitchExpression.

Ejemplos

En el ejemplo siguiente se muestra cómo crear una expresión que representa una instrucción switch que tiene un caso predeterminado.

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

Comentarios

Todos los SwitchCase objetos de un SwitchExpression objeto deben tener el mismo tipo, a menos que SwitchExpression tenga el tipo void.

Cada SwitchCase objeto tiene una instrucción implícita break , lo que significa que no hay ninguna caída implícita de una etiqueta de mayúsculas y minúsculas a otra.

Si switchValue no coincide con ninguno de los casos, se ejecuta el caso predeterminado representado por defaultBody .

Se aplica a

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

Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Parámetros

switchValue
Expression

Valor que se va a probar en cada caso.

defaultBody
Expression

Resultado del modificador si switchValue no coincide con ninguno de los casos.

comparison
MethodInfo

Método de comparación de igualdad que se va a usar.

cases
IEnumerable<SwitchCase>

Conjunto de casos para esta expresión switch.

Devoluciones

Objeto creado SwitchExpression.

Se aplica a

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

Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Parámetros

switchValue
Expression

Valor que se va a probar en cada caso.

defaultBody
Expression

Resultado del modificador si switchValue no coincide con ninguno de los casos.

comparison
MethodInfo

Método de comparación de igualdad que se va a usar.

cases
SwitchCase[]

Conjunto de casos para esta expresión switch.

Devoluciones

Objeto creado SwitchExpression.

Se aplica a

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

Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs
Source:
SwitchExpression.cs

Crea un SwitchExpression objeto que representa una switch instrucción que tiene un caso predeterminado.

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

Parámetros

type
Type

Tipo de resultado del modificador.

switchValue
Expression

Valor que se va a probar en cada caso.

defaultBody
Expression

Resultado del modificador si switchValue no coincide con ninguno de los casos.

comparison
MethodInfo

Método de comparación de igualdad que se va a usar.

cases
IEnumerable<SwitchCase>

Conjunto de casos para esta expresión switch.

Devoluciones

Objeto creado SwitchExpression.

Se aplica a