Compartir a través de


Expression.SwitchCase Método

Definición

Crea un objeto SwitchCase que se va a usar en un objeto SwitchExpression.

Sobrecargas

SwitchCase(Expression, Expression[])

Crea un objeto SwitchCase que se va a usar en un objeto SwitchExpression.

SwitchCase(Expression, IEnumerable<Expression>)

Crea un objeto SwitchCase que se va a usar en un objeto SwitchExpression.

SwitchCase(Expression, Expression[])

Source:
SwitchCase.cs
Source:
SwitchCase.cs
Source:
SwitchCase.cs

Crea un objeto SwitchCase que se va a usar en un objeto SwitchExpression.

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

Parámetros

body
Expression

Cuerpo del caso.

testValues
Expression[]

Valores de prueba del caso.

Devoluciones

Objeto SwitchCase creado.

Se aplica a

SwitchCase(Expression, IEnumerable<Expression>)

Source:
SwitchCase.cs
Source:
SwitchCase.cs
Source:
SwitchCase.cs

Crea un objeto SwitchCase que se va a usar en un objeto SwitchExpression.

public:
 static System::Linq::Expressions::SwitchCase ^ SwitchCase(System::Linq::Expressions::Expression ^ body, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ testValues);
public static System.Linq.Expressions.SwitchCase SwitchCase (System.Linq.Expressions.Expression body, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> testValues);
static member SwitchCase : System.Linq.Expressions.Expression * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.SwitchCase
Public Shared Function SwitchCase (body As Expression, testValues As IEnumerable(Of Expression)) As SwitchCase

Parámetros

body
Expression

Cuerpo del caso.

testValues
IEnumerable<Expression>

Valores de prueba del caso.

Devoluciones

Objeto SwitchCase creado.

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 SwitchExpression que 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.

Se aplica a