SwitchExpression Classe

Definizione

Rappresenta un'espressione di controllo che gestisce più selezioni passando il controllo a 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
Ereditarietà
SwitchExpression

Esempio

Nell'esempio seguente viene illustrato come creare un'espressione che rappresenta un'istruzione switch con un case predefinito usando il Switch metodo .

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

Proprietà

Nome Descrizione
CanReduce

Indica che il nodo può essere ridotto a un nodo più semplice. Se restituisce true, è possibile chiamare Reduce() per produrre la forma ridotta.

(Ereditato da Expression)
Cases

Ottiene la raccolta di SwitchCase oggetti per l'opzione.

Comparison

Ottiene il metodo di confronto di uguaglianza, se presente.

DefaultBody

Ottiene il test per l'opzione.

NodeType

Restituisce il tipo di nodo di questa espressione. I nodi di estensione devono restituire Extension quando si esegue l'override di questo metodo.

SwitchValue

Ottiene il test per l'opzione.

Type

Ottiene il tipo statico dell'espressione rappresentata da .Expression

Metodi

Nome Descrizione
Accept(ExpressionVisitor)

Invia al metodo visit specifico per questo tipo di nodo. Ad esempio, MethodCallExpression chiama .VisitMethodCall(MethodCallExpression)

(Ereditato da Expression)
Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
Reduce()

Riduce questo nodo a un'espressione più semplice. Se CanReduce restituisce true, verrà restituita un'espressione valida. Questo metodo può restituire un altro nodo che deve essere ridotto.

(Ereditato da Expression)
ReduceAndCheck()

Riduce questo nodo a un'espressione più semplice. Se CanReduce restituisce true, verrà restituita un'espressione valida. Questo metodo può restituire un altro nodo che deve essere ridotto.

(Ereditato da Expression)
ReduceExtensions()

Riduce l'espressione a un tipo di nodo noto (che non è un nodo di estensione) o restituisce semplicemente l'espressione se è già un tipo noto.

(Ereditato da Expression)
ToString()

Restituisce una rappresentazione testuale dell'oggetto Expression.

(Ereditato da Expression)
Update(Expression, IEnumerable<SwitchCase>, Expression)

Crea una nuova espressione simile a questa, ma utilizzando gli elementi figlio forniti. Se tutti gli elementi figlio sono uguali, restituirà questa espressione.

VisitChildren(ExpressionVisitor)

Riduce il nodo e quindi chiama il delegato visitor nell'espressione ridotta. Il metodo genera un'eccezione se il nodo non è reducibile.

(Ereditato da Expression)

Si applica a