SwitchExpression 類別
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示透過將控制權傳遞至 SwitchCase 來處理多重選擇的控制運算式。
public ref class SwitchExpression sealed : System::Linq::Expressions::Expression
public sealed class SwitchExpression : System.Linq.Expressions.Expression
type SwitchExpression = class
inherit Expression
Public NotInheritable Class SwitchExpression
Inherits Expression
- 繼承
下列範例示範如何建立表達式,此表達式代表使用 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
Can |
表示節點可精簡為更簡單的節點。 如果傳回 true,則可呼叫 Reduce() 以產生精簡的形式。 (繼承來源 Expression) |
Cases |
取得切換的 SwitchCase 物件集合。 |
Comparison |
取得相等比較方法 (如果有的話)。 |
Default |
取得切換的測試。 |
Node |
傳回這個 Expression 的節點型別。 覆寫此方法時,擴充節點應傳回 Extension。 |
Switch |
取得切換的測試。 |
Type |
取得此 Expression 代表之運算式的靜態類型。 |
Accept(Expression |
分派給這個節點類型的特定造訪方法。 例如,MethodCallExpression 會呼叫 VisitMethodCall(MethodCallExpression)。 (繼承來源 Expression) |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Get |
做為預設雜湊函式。 (繼承來源 Object) |
Get |
取得目前執行個體的 Type。 (繼承來源 Object) |
Memberwise |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Reduce() |
將這個節點精簡為更簡單的運算式。 如果 CanReduce 傳回 true,則應該傳回有效的運算式。 這個方法可以傳回其他本身必須精簡的節點。 (繼承來源 Expression) |
Reduce |
將這個節點精簡為更簡單的運算式。 如果 CanReduce 傳回 true,則應該傳回有效的運算式。 這個方法可以傳回其他本身必須精簡的節點。 (繼承來源 Expression) |
Reduce |
將運算式精簡為已知的節點類型 (不是 Extension 節點),如果已經是已知的類型,則僅傳回運算式。 (繼承來源 Expression) |
To |
傳回 Expression 的文字表示。 (繼承來源 Expression) |
Update(Expression, IEnumerable<Switch |
建立類似這個運算式的新運算式,但使用提供的子系。 如果所有子系都相同,則會傳回這個運算式。 |
Visit |
精簡節點,然後呼叫精簡後的運算式上的訪問項委派。 如果節點無法縮減,此方法會擲回例外狀況。 (繼承來源 Expression) |
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |