Expression.Switch Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Vytvoří příkaz SwitchExpression , který představuje switch příkaz.
Přetížení
| Name | Description |
|---|---|
| Switch(Type, Expression, Expression, MethodInfo, SwitchCase[]) |
Vytvoří SwitchExpression příkaz, který |
| Switch(Expression, SwitchCase[]) |
SwitchExpression Vytvoří příkaz, který představuje |
| Switch(Expression, Expression, SwitchCase[]) |
Vytvoří SwitchExpression příkaz, který |
| Switch(Expression, Expression, MethodInfo, IEnumerable<SwitchCase>) |
Vytvoří SwitchExpression příkaz, který |
| Switch(Expression, Expression, MethodInfo, SwitchCase[]) |
Vytvoří SwitchExpression příkaz, který |
| Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>) |
Vytvoří SwitchExpression příkaz, který |
Switch(Type, Expression, Expression, MethodInfo, SwitchCase[])
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
Vytvoří SwitchExpression příkaz, který switch má výchozí případ.
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
Parametry
- type
- Type
Typ výsledku přepínače.
- switchValue
- Expression
Hodnota, která se má testovat proti jednotlivým případům.
- defaultBody
- Expression
Výsledek přepínače, pokud switchValue neodpovídá žádnému z případů.
- comparison
- MethodInfo
Metoda porovnání rovnosti, která se má použít.
- cases
- SwitchCase[]
Sada případů pro tento výraz přepínače.
Návraty
SwitchExpressionVytvořeno .
Platí pro
Switch(Expression, SwitchCase[])
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
SwitchExpression Vytvoří příkaz, který představuje switch příkaz bez výchozího případu.
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
Parametry
- switchValue
- Expression
Hodnota, která se má testovat proti jednotlivým případům.
- cases
- SwitchCase[]
Sada případů pro tento výraz přepínače.
Návraty
SwitchExpressionVytvořeno .
Příklady
Následující příklad ukazuje, jak vytvořit výraz, který představuje příkaz switch bez výchozího případu.
// 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
Poznámky
Všechny SwitchCase objekty v objektu SwitchExpression musí mít stejný typ, pokud SwitchExpression typ nemá void.
Každý SwitchCase objekt má implicitní break příkaz, což znamená, že neexistuje žádný implicitní pád z jednoho popisku případu do druhého.
Pokud switchValue neodpovídá žádnému z případů, nevyvolá se žádná výjimka.
Platí pro
Switch(Expression, Expression, SwitchCase[])
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
Vytvoří SwitchExpression příkaz, který switch má výchozí případ.
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
Parametry
- switchValue
- Expression
Hodnota, která se má testovat proti jednotlivým případům.
- defaultBody
- Expression
Výsledek přepínače, pokud switchValue neodpovídá žádnému z případů.
- cases
- SwitchCase[]
Sada případů pro tento výraz přepínače.
Návraty
SwitchExpressionVytvořeno .
Příklady
Následující příklad ukazuje, jak vytvořit výraz, který představuje příkaz switch, který má výchozí případ.
// 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
Poznámky
Všechny SwitchCase objekty v objektu SwitchExpression musí mít stejný typ, pokud SwitchExpression typ nemá void.
Každý SwitchCase objekt má implicitní break příkaz, což znamená, že neexistuje žádný implicitní pád z jednoho popisku případu do druhého.
Pokud switchValue neodpovídá žádnému z případů, spustí se výchozí případ reprezentovaný defaultBody .
Platí pro
Switch(Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
Vytvoří SwitchExpression příkaz, který switch má výchozí případ.
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
Parametry
- switchValue
- Expression
Hodnota, která se má testovat proti jednotlivým případům.
- defaultBody
- Expression
Výsledek přepínače, pokud switchValue neodpovídá žádnému z případů.
- comparison
- MethodInfo
Metoda porovnání rovnosti, která se má použít.
- cases
- IEnumerable<SwitchCase>
Sada případů pro tento výraz přepínače.
Návraty
SwitchExpressionVytvořeno .
Platí pro
Switch(Expression, Expression, MethodInfo, SwitchCase[])
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
Vytvoří SwitchExpression příkaz, který switch má výchozí případ.
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
Parametry
- switchValue
- Expression
Hodnota, která se má testovat proti jednotlivým případům.
- defaultBody
- Expression
Výsledek přepínače, pokud switchValue neodpovídá žádnému z případů.
- comparison
- MethodInfo
Metoda porovnání rovnosti, která se má použít.
- cases
- SwitchCase[]
Sada případů pro tento výraz přepínače.
Návraty
SwitchExpressionVytvořeno .
Platí pro
Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
- Zdroj:
- SwitchExpression.cs
Vytvoří SwitchExpression příkaz, který switch má výchozí případ.
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
Parametry
- type
- Type
Typ výsledku přepínače.
- switchValue
- Expression
Hodnota, která se má testovat proti jednotlivým případům.
- defaultBody
- Expression
Výsledek přepínače, pokud switchValue neodpovídá žádnému z případů.
- comparison
- MethodInfo
Metoda porovnání rovnosti, která se má použít.
- cases
- IEnumerable<SwitchCase>
Sada případů pro tento výraz přepínače.
Návraty
SwitchExpressionVytvořeno .