Freigeben über


Expression.ListInit Method (NewExpression, MethodInfo, array<Expression[])

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

Creates a ListInitExpression that uses a specified method to add elements to a collection.

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

Syntax

'Declaration
Public Shared Function ListInit ( _
    newExpression As NewExpression, _
    addMethod As MethodInfo, _
    ParamArray initializers As Expression() _
) As ListInitExpression
public static ListInitExpression ListInit(
    NewExpression newExpression,
    MethodInfo addMethod,
    params Expression[] initializers
)

Parameters

Return Value

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

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.

-or-

addMethod is not nulla null reference (Nothing in Visual Basic) and it does not represent an instance method named "Add" (case insensitive) that takes exactly one argument.

-or-

addMethod is not nulla null reference (Nothing in Visual Basic) and the type represented by the Type property of one or more elements of initializers is not assignable to the argument type of the method that addMethod represents.

InvalidOperationException

addMethod is nulla null reference (Nothing in Visual Basic) and no instance method named "Add" that takes one type-compatible argument exists on newExpression.Type or its base type.

Remarks

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

If addMethod is nulla null reference (Nothing in Visual Basic), newExpression.Type or its base type must declare a single method named "Add" (case insensitive) that takes exactly one argument. If addMethod is not nulla null reference (Nothing in Visual Basic), it must represent an instance method named "Add" (case insensitive) that has exactly one parameter. The type represented by the Type property of each element of initializers must be assignable to the argument type of the add method.

The Initializers property of the returned ListInitExpression contains one element of type ElementInit for each element of initializers. The Arguments property of each element of Initializers is a singleton collection that contains the corresponding element of initializers. The AddMethod property of each element of Initializers is equal to addMethod.

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

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.