SwitchExpression Klasa

Definicja

Reprezentuje wyrażenie sterujące, które obsługuje wiele wyborów przez przekazanie kontrolki do elementu 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
Dziedziczenie
SwitchExpression

Przykłady

W poniższym przykładzie pokazano, jak utworzyć wyrażenie reprezentujące instrukcję switch, która ma domyślny przypadek przy użyciu Switch metody .

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

Właściwości

CanReduce

Wskazuje, że węzeł można zmniejszyć do prostszego węzła. Jeśli zwraca wartość true, funkcja Reduce() może zostać wywołana w celu utworzenia skróconej postaci.

(Odziedziczone po Expression)
Cases

Pobiera kolekcję SwitchCase obiektów dla przełącznika.

Comparison

Pobiera metodę porównania równości, jeśli istnieje.

DefaultBody

Pobiera test przełącznika.

NodeType

Zwraca typ węzła tego wyrażenia. Węzły rozszerzeń powinny zwracać Extension podczas zastępowania tej metody.

SwitchValue

Pobiera test przełącznika.

Type

Pobiera statyczny typ wyrażenia, które reprezentuje Expression .

Metody

Accept(ExpressionVisitor)

Wysyła do określonej metody visit dla tego typu węzła. Na przykład MethodCallExpression wywołuje metodę VisitMethodCall(MethodCallExpression).

(Odziedziczone po Expression)
Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
Reduce()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinno to zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który należy zmniejszyć.

(Odziedziczone po Expression)
ReduceAndCheck()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinno to zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który należy zmniejszyć.

(Odziedziczone po Expression)
ReduceExtensions()

Zmniejsza wyrażenie do znanego typu węzła (który nie jest węzłem rozszerzenia) lub po prostu zwraca wyrażenie, jeśli jest to już znany typ.

(Odziedziczone po Expression)
ToString()

Zwraca tekstową reprezentację obiektu Expression.

(Odziedziczone po Expression)
Update(Expression, IEnumerable<SwitchCase>, Expression)

Tworzy nowe wyrażenie, które jest podobne do tego, ale przy użyciu dostarczonych elementów podrzędnych. Jeśli wszystkie elementy podrzędne są takie same, zwróci to wyrażenie.

VisitChildren(ExpressionVisitor)

Zmniejsza węzeł, a następnie wywołuje delegata odwiedzających w wyrażeniu zredukowanym. Metoda zgłasza wyjątek, jeśli węzeł nie jest reducible.

(Odziedziczone po Expression)

Dotyczy