Sdílet prostřednictvím


ListInitExpression Třída

Definice

Představuje volání konstruktoru, který má inicializátor kolekce.

public ref class ListInitExpression sealed : System::Linq::Expressions::Expression
public sealed class ListInitExpression : System.Linq.Expressions.Expression
type ListInitExpression = class
    inherit Expression
Public NotInheritable Class ListInitExpression
Inherits Expression
Dědičnost
ListInitExpression

Příklady

Následující příklad vytvoří ListInitExpression inicializaci nové instance slovníku, která má dva páry klíč-hodnota.

string tree1 = "maple";
string tree2 = "oak";

System.Reflection.MethodInfo addMethod = typeof(Dictionary<int, string>).GetMethod("Add");

// Create two ElementInit objects that represent the
// two key-value pairs to add to the Dictionary.
System.Linq.Expressions.ElementInit elementInit1 =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree1.Length),
        System.Linq.Expressions.Expression.Constant(tree1));
System.Linq.Expressions.ElementInit elementInit2 =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree2.Length),
        System.Linq.Expressions.Expression.Constant(tree2));

// Create a NewExpression that represents constructing
// a new instance of Dictionary<int, string>.
System.Linq.Expressions.NewExpression newDictionaryExpression =
    System.Linq.Expressions.Expression.New(typeof(Dictionary<int, string>));

// Create a ListInitExpression that represents initializing
// a new Dictionary<> instance with two key-value pairs.
System.Linq.Expressions.ListInitExpression listInitExpression =
    System.Linq.Expressions.Expression.ListInit(
        newDictionaryExpression,
        elementInit1,
        elementInit2);

Console.WriteLine(listInitExpression.ToString());

// This code produces the following output:
//
// new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"),
// Void Add(Int32, System.String)(3,"oak")}
Dim tree1 As String = "maple"
Dim tree2 As String = "oak"

Dim addMethod As System.Reflection.MethodInfo = _
    Type.GetType("System.Collections.Generic.Dictionary`2[System.Int32, System.String]").GetMethod("Add")

' Create two ElementInit objects that represent the
' two key-value pairs to add to the Dictionary.
Dim elementInit1 As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree1.Length), _
        System.Linq.Expressions.Expression.Constant(tree1))
Dim elementInit2 As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree2.Length), _
        System.Linq.Expressions.Expression.Constant(tree2))

' Create a NewExpression that represents constructing
' a new instance of Dictionary(Of Integer, String).
Dim newDictionaryExpression As System.Linq.Expressions.NewExpression = _
    System.Linq.Expressions.Expression.[New](Type.GetType("System.Collections.Generic.Dictionary`2[System.Int32, System.String]"))

' Create a ListInitExpression that represents initializing
' a new Dictionary(Of T) instance with two key-value pairs.
Dim listInitExpression As System.Linq.Expressions.ListInitExpression = _
    System.Linq.Expressions.Expression.ListInit( _
        newDictionaryExpression, _
        elementInit1, _
        elementInit2)

Console.WriteLine(listInitExpression.ToString())

' This code produces the following output:
'
' new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"),
' Void Add(Int32, System.String)(3,"oak")

Poznámky

K vytvoření objektu ListInitListInitExpressionpoužijte metody továrny .

Hodnota NodeType vlastnosti je ListInitExpressionListInit.

Vlastnosti

Name Description
CanReduce

Získá hodnotu, která označuje, zda je možné snížit uzel stromu výrazu.

Initializers

Získá inicializátory elementu, které se používají k inicializaci kolekce.

NewExpression

Získá výraz, který obsahuje volání konstruktoru typu kolekce.

NodeType

Vrátí typ uzlu tohoto Expression.

NodeType

Získá typ uzlu tohoto Expression.

(Zděděno od Expression)
Type

Získá statický typ výrazu, který představuje Expression .

Type

Získá statický typ výrazu, který představuje Expression .

(Zděděno od Expression)

Metody

Name Description
Accept(ExpressionVisitor)

Odešle konkrétní metodu návštěvy pro tento typ uzlu. MethodCallExpression Například volá .VisitMethodCall(MethodCallExpression)

(Zděděno od Expression)
Equals(Object)

Určuje, zda je zadaný objekt roven aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetType()

Získá Type aktuální instance.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Object.

(Zděděno od Object)
Reduce()

Zmenší uzel binárního výrazu na jednodušší výraz.

ReduceAndCheck()

Zmenší tento uzel na jednodušší výraz. Pokud CanReduce vrátí hodnotu true, měl by vrátit platný výraz. Tato metoda může vrátit jiný uzel, který se musí snížit.

(Zděděno od Expression)
ReduceExtensions()

Zmenšuje výraz na známý typ uzlu (který není uzel rozšíření), nebo pouze vrátí výraz, pokud je již známým typem.

(Zděděno od Expression)
ToString()

Vrátí textovou reprezentaci Expression.

(Zděděno od Expression)
Update(NewExpression, IEnumerable<ElementInit>)

Vytvoří nový výraz, který je podobný tomuto, ale použije zadané podřízené položky. Pokud jsou všechny podřízené položky stejné, vrátí tento výraz.

VisitChildren(ExpressionVisitor)

Zmenší uzel a potom zavolá delegáta návštěvníka na zkrácený výraz. Metoda vyvolá výjimku, pokud uzel není reducible.

(Zděděno od Expression)

Platí pro