Expression.Switch 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建一个表示 SwitchExpression 语句的 switch。
重载
| Switch(Type, Expression, Expression, MethodInfo, SwitchCase[]) | 创建一个表示具有默认分支的 SwitchExpression 语句的  | 
| Switch(Expression, SwitchCase[]) | 创建一个表示不带默认分支的 SwitchExpression 语句的  | 
| Switch(Expression, Expression, SwitchCase[]) | 创建一个表示具有默认分支的 SwitchExpression 语句的  | 
| Switch(Expression, Expression, MethodInfo, IEnumerable<SwitchCase>) | 创建一个表示具有默认分支的 SwitchExpression 语句的  | 
| Switch(Expression, Expression, MethodInfo, SwitchCase[]) | 创建一个表示具有默认分支的 SwitchExpression 语句的  | 
| Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>) | 创建一个表示具有默认分支的 SwitchExpression 语句的  | 
Switch(Type, Expression, Expression, MethodInfo, SwitchCase[])
- Source:
- SwitchExpression.cs
- Source:
- SwitchExpression.cs
- Source:
- 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.SwitchExpressionPublic Shared Function Switch (type As Type, switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, ParamArray cases As SwitchCase()) As SwitchExpression参数
- type
- Type
switch 的结果类型。
- switchValue
- Expression
要针对每个分支测试的值。
- defaultBody
- Expression
              switchValue 不匹配任何分支时,该 switch 的结果。
- comparison
- MethodInfo
要使用的相等比较方法。
- cases
- SwitchCase[]
此 switch 表达式的分支集。
返回
创建的 SwitchExpression。
适用于
Switch(Expression, SwitchCase[])
- Source:
- SwitchExpression.cs
- Source:
- SwitchExpression.cs
- Source:
- SwitchExpression.cs
创建一个表示不带默认分支的 SwitchExpression 语句的 switch。
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.SwitchExpressionPublic Shared Function Switch (switchValue As Expression, ParamArray cases As SwitchCase()) As SwitchExpression参数
- switchValue
- Expression
要针对每个分支测试的值。
- cases
- SwitchCase[]
此 switch 表达式的分支集。
返回
创建的 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[])
- Source:
- SwitchExpression.cs
- Source:
- SwitchExpression.cs
- Source:
- 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.SwitchExpressionPublic Shared Function Switch (switchValue As Expression, defaultBody As Expression, ParamArray cases As SwitchCase()) As SwitchExpression参数
- switchValue
- Expression
要针对每个分支测试的值。
- defaultBody
- Expression
              switchValue 不匹配任何分支时,该 switch 的结果。
- cases
- SwitchCase[]
此 switch 表达式的分支集。
返回
创建的 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>)
- Source:
- SwitchExpression.cs
- Source:
- SwitchExpression.cs
- Source:
- 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.SwitchExpressionPublic Shared Function Switch (switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, cases As IEnumerable(Of SwitchCase)) As SwitchExpression参数
- switchValue
- Expression
要针对每个分支测试的值。
- defaultBody
- Expression
              switchValue 不匹配任何分支时,该 switch 的结果。
- comparison
- MethodInfo
要使用的相等比较方法。
- cases
- IEnumerable<SwitchCase>
此 switch 表达式的分支集。
返回
创建的 SwitchExpression。
适用于
Switch(Expression, Expression, MethodInfo, SwitchCase[])
- Source:
- SwitchExpression.cs
- Source:
- SwitchExpression.cs
- Source:
- 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.SwitchExpressionPublic Shared Function Switch (switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, ParamArray cases As SwitchCase()) As SwitchExpression参数
- switchValue
- Expression
要针对每个分支测试的值。
- defaultBody
- Expression
              switchValue 不匹配任何分支时,该 switch 的结果。
- comparison
- MethodInfo
要使用的相等比较方法。
- cases
- SwitchCase[]
此 switch 表达式的分支集。
返回
创建的 SwitchExpression。
适用于
Switch(Type, Expression, Expression, MethodInfo, IEnumerable<SwitchCase>)
- Source:
- SwitchExpression.cs
- Source:
- SwitchExpression.cs
- Source:
- 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.SwitchExpressionPublic Shared Function Switch (type As Type, switchValue As Expression, defaultBody As Expression, comparison As MethodInfo, cases As IEnumerable(Of SwitchCase)) As SwitchExpression参数
- type
- Type
switch 的结果类型。
- switchValue
- Expression
要针对每个分支测试的值。
- defaultBody
- Expression
              switchValue 不匹配任何分支时,该 switch 的结果。
- comparison
- MethodInfo
要使用的相等比较方法。
- cases
- IEnumerable<SwitchCase>
此 switch 表达式的分支集。
返回
创建的 SwitchExpression。