Expression.Switch Metod

Definition

Skapar en SwitchExpression som representerar en switch -instruktion.

Överlagringar

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

Switch(Expression, SwitchCase[])

Skapar en SwitchExpression som representerar en switch -instruktion utan standardfall.

Switch(Expression, Expression, SwitchCase[])

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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);
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

Parametrar

type
Type

Resultattypen för växeln.

switchValue
Expression

Värdet som ska testas mot varje ärende.

defaultBody
Expression

Resultatet av växeln om switchValue inte matchar något av fallen.

comparison
MethodInfo

Likhetsjämförelsemetoden som ska användas.

cases
SwitchCase[]

Uppsättningen fall för det här växeluttrycket.

Returer

Den skapade SwitchExpression.

Gäller för

Switch(Expression, SwitchCase[])

Skapar en SwitchExpression som representerar en switch -instruktion utan standardfall.

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);
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

Parametrar

switchValue
Expression

Värdet som ska testas mot varje ärende.

cases
SwitchCase[]

Uppsättningen fall för det här växeluttrycket.

Returer

Den skapade SwitchExpression.

Exempel

I följande exempel visas hur du skapar ett uttryck som representerar en switch-instruktion utan standardfall.

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

Kommentarer

Alla SwitchCase objekt i ett SwitchExpression objekt måste ha samma typ, såvida inte SwitchExpression har typen void.

Varje SwitchCase objekt har en implicit break -instruktion, vilket innebär att det inte finns någon implicit sänkning från en ärendeetikett till en annan.

Om switchValue inte matchar något av fallen utlöses inget undantag.

Gäller för

Switch(Expression, Expression, SwitchCase[])

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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);
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

Parametrar

switchValue
Expression

Värdet som ska testas mot varje ärende.

defaultBody
Expression

Resultatet av växeln om switchValue inte matchar något av fallen.

cases
SwitchCase[]

Uppsättningen fall för det här växeluttrycket.

Returer

Den skapade SwitchExpression.

Exempel

I följande exempel visas hur du skapar ett uttryck som representerar en switch-instruktion som har ett standardfall.

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

Kommentarer

Alla SwitchCase objekt i ett SwitchExpression objekt måste ha samma typ, såvida inte SwitchExpression har typen void.

Varje SwitchCase objekt har en implicit break -instruktion, vilket innebär att det inte finns någon implicit sänkning från en ärendeetikett till en annan.

Om switchValue inte matchar något av fallen körs standardfallet som representeras av defaultBody .

Gäller för

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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

Parametrar

switchValue
Expression

Värdet som ska testas mot varje ärende.

defaultBody
Expression

Resultatet av växeln om switchValue inte matchar något av fallen.

comparison
MethodInfo

Likhetsjämförelsemetoden som ska användas.

cases
IEnumerable<SwitchCase>

Uppsättningen fall för det här växeluttrycket.

Returer

Den skapade SwitchExpression.

Gäller för

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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);
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

Parametrar

switchValue
Expression

Värdet som ska testas mot varje ärende.

defaultBody
Expression

Resultatet av växeln om switchValue inte matchar något av fallen.

comparison
MethodInfo

Likhetsjämförelsemetoden som ska användas.

cases
SwitchCase[]

Uppsättningen fall för det här växeluttrycket.

Returer

Den skapade SwitchExpression.

Gäller för

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

Skapar en SwitchExpression som representerar en switch -instruktion som har ett standardfall.

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

Parametrar

type
Type

Resultattypen för växeln.

switchValue
Expression

Värdet som ska testas mot varje ärende.

defaultBody
Expression

Resultatet av växeln om switchValue inte matchar något av fallen.

comparison
MethodInfo

Likhetsjämförelsemetoden som ska användas.

cases
IEnumerable<SwitchCase>

Uppsättningen fall för det här växeluttrycket.

Returer

Den skapade SwitchExpression.

Gäller för