Udostępnij za pośrednictwem


Expression.ListInit Method (NewExpression, IEnumerable<ElementInit>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a ListInitExpression that uses specified ElementInit objects to initialize a collection.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function ListInit ( _
    newExpression As NewExpression, _
    initializers As IEnumerable(Of ElementInit) _
) As ListInitExpression
public static ListInitExpression ListInit(
    NewExpression newExpression,
    IEnumerable<ElementInit> initializers
)

Parameters

Return Value

Type: System.Linq.Expressions.ListInitExpression
A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression and Initializers properties set to the specified values.

Exceptions

Exception Condition
ArgumentNullException

newExpression or initializers is nulla null reference (Nothing in Visual Basic).

-or-

One or more elements of initializers are nulla null reference (Nothing in Visual Basic).

ArgumentException

newExpression.Type does not implement IEnumerable.

Remarks

The Type property of newExpression must represent a type that implements IEnumerable.

The Type property of the resulting ListInitExpression is equal to newExpression.Type.

Examples

The following example demonstrates how to use the ListInit(NewExpression, array<ElementInit[]) method to create a ListInitExpression that represents the initialization of a new dictionary instance with two key-value pairs.

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)

outputBlock.Text &= listInitExpression.ToString() & vbCrLf

' This code produces the following output:
'
' new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"),
' Void Add(Int32, System.String)(3,"oak")
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);

outputBlock.Text += listInitExpression.ToString() + "\n";

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.