Export (0) Print
Expand All

Expression.ListInit Method (NewExpression, MethodInfo, Expression())

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)

'Declaration
Public Shared Function ListInit ( _
	newExpression As NewExpression, _
	addMethod As MethodInfo, _
	ParamArray initializers As Expression() _
) As ListInitExpression

Parameters

newExpression
Type: System.Linq.Expressions.NewExpression

A NewExpression to set the NewExpression property equal to.

addMethod
Type: System.Reflection.MethodInfo

A MethodInfo that represents an instance method that takes one argument, that adds an element to a collection.

initializers
Type: System.Linq.Expressions.Expression()

An array of Expression objects to use to populate the Initializers collection.

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.

ExceptionCondition
ArgumentNullException

newExpression or initializers is Nothing.

-or-

One or more elements of initializers are Nothing.

ArgumentException

newExpression.Type does not implement IEnumerable.

-or-

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

-or-

addMethod is not Nothing 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 Nothing and no instance method named "Add" that takes one type-compatible argument exists on newExpression.Type or its base type.

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

If addMethod is Nothing, newExpression.Type or its base type must declare a single method named "Add" (case insensitive) that takes exactly one argument. If addMethod is not Nothing, 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.

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft