Compartilhar via


ListInitExpression Classe

Definição

Representa uma chamada de construtor que tem um inicializador de coleção.

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
Herança
ListInitExpression

Exemplos

O exemplo a seguir cria um ListInitExpression que representa a inicialização de uma nova instância de dicionário que tem dois pares chave-valor.

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")

Comentários

Use os métodos de ListInit fábrica para criar um ListInitExpression.

O valor da NodeType propriedade de a ListInitExpression é ListInit.

Propriedades

Nome Description
CanReduce

Obtém um valor que indica se o nó da árvore de expressão pode ser reduzido.

Initializers

Obtém os inicializadores de elemento usados para inicializar uma coleção.

NewExpression

Obtém a expressão que contém uma chamada para o construtor de um tipo de coleção.

NodeType

Retorna o tipo de nó deste Expression.

NodeType

Obtém o tipo de nó deste Expression.

(Herdado de Expression)
Type

Obtém o tipo estático da expressão que isso Expression representa.

Type

Obtém o tipo estático da expressão que isso Expression representa.

(Herdado de Expression)

Métodos

Nome Description
Accept(ExpressionVisitor)

Envia para o método de visita específico para esse tipo de nó. Por exemplo, MethodCallExpression chama o VisitMethodCall(MethodCallExpression).

(Herdado de Expression)
Equals(Object)

Determina se o objeto especificado é igual ao objeto atual.

(Herdado de Object)
GetHashCode()

Serve como a função de hash padrão.

(Herdado de Object)
GetType()

Obtém o Type da instância atual.

(Herdado de Object)
MemberwiseClone()

Cria uma cópia superficial do Objectatual.

(Herdado de Object)
Reduce()

Reduz o nó de expressão binária a uma expressão mais simples.

ReduceAndCheck()

Reduz esse nó a uma expressão mais simples. Se CanReduce retornar true, isso deverá retornar uma expressão válida. Esse método pode retornar outro nó que deve ser reduzido.

(Herdado de Expression)
ReduceExtensions()

Reduz a expressão a um tipo de nó conhecido (que não é um nó de extensão) ou apenas retorna a expressão se já for um tipo conhecido.

(Herdado de Expression)
ToString()

Retorna uma representação textual do Expression.

(Herdado de Expression)
Update(NewExpression, IEnumerable<ElementInit>)

Cria uma nova expressão semelhante a esta, mas usando os filhos fornecidos. Se todas as crianças forem iguais, ela retornará essa expressão.

VisitChildren(ExpressionVisitor)

Reduz o nó e, em seguida, chama o delegado visitante na expressão reduzida. O método gerará uma exceção se o nó não for reduível.

(Herdado de Expression)

Aplica-se a