Redigera

Dela via


Expression.ListInit Method

Definition

Creates a ListInitExpression.

Overloads

ListInit(NewExpression, IEnumerable<ElementInit>)

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

ListInit(NewExpression, IEnumerable<Expression>)

Creates a ListInitExpression that uses a method named "Add" to add elements to a collection.

ListInit(NewExpression, ElementInit[])

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

ListInit(NewExpression, Expression[])

Creates a ListInitExpression that uses a method named "Add" to add elements to a collection.

ListInit(NewExpression, MethodInfo, IEnumerable<Expression>)

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

ListInit(NewExpression, MethodInfo, Expression[])

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

ListInit(NewExpression, IEnumerable<ElementInit>)

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

public:
 static System::Linq::Expressions::ListInitExpression ^ ListInit(System::Linq::Expressions::NewExpression ^ newExpression, System::Collections::Generic::IEnumerable<System::Linq::Expressions::ElementInit ^> ^ initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ElementInit> initializers);
static member ListInit : System.Linq.Expressions.NewExpression * seq<System.Linq.Expressions.ElementInit> -> System.Linq.Expressions.ListInitExpression
Public Shared Function ListInit (newExpression As NewExpression, initializers As IEnumerable(Of ElementInit)) As ListInitExpression

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
IEnumerable<ElementInit>

An IEnumerable<T> that contains ElementInit objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression and Initializers properties set to the specified values.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

Examples

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

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

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.

Applies to

ListInit(NewExpression, IEnumerable<Expression>)

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

Creates a ListInitExpression that uses a method named "Add" to add elements to a collection.

public:
 static System::Linq::Expressions::ListInitExpression ^ ListInit(System::Linq::Expressions::NewExpression ^ newExpression, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);
static member ListInit : System.Linq.Expressions.NewExpression * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.ListInitExpression
Public Shared Function ListInit (newExpression As NewExpression, initializers As IEnumerable(Of Expression)) As ListInitExpression

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

There is no instance method named "Add" (case insensitive) declared in newExpression.Type or its base type.

-or-

The add method on newExpression.Type or its base type does not take exactly one argument.

-or-

The type represented by the Type property of the first element of initializers is not assignable to the argument type of the add method on newExpression.Type or its base type.

-or-

More than one argument-compatible method named "Add" (case-insensitive) exists on newExpression.Type and/or its base type.

Remarks

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

In order to use this overload of ListInit(NewExpression, IEnumerable<Expression>), newExpression.Type or its base type must declare a single method named "Add" (case insensitive) that takes exactly one argument. The type of the argument must be assignable from the type represented by the Type property of the first element of initializers.

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 represents the add method that was discovered on newExpression.Type or its base type.

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

Applies to

ListInit(NewExpression, ElementInit[])

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

public:
 static System::Linq::Expressions::ListInitExpression ^ ListInit(System::Linq::Expressions::NewExpression ^ newExpression, ... cli::array <System::Linq::Expressions::ElementInit ^> ^ initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, params System.Linq.Expressions.ElementInit[] initializers);
static member ListInit : System.Linq.Expressions.NewExpression * System.Linq.Expressions.ElementInit[] -> System.Linq.Expressions.ListInitExpression
Public Shared Function ListInit (newExpression As NewExpression, ParamArray initializers As ElementInit()) As ListInitExpression

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
ElementInit[]

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

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression and Initializers properties set to the specified values.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

Examples

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

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

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.

Applies to

ListInit(NewExpression, Expression[])

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

Creates a ListInitExpression that uses a method named "Add" to add elements to a collection.

public:
 static System::Linq::Expressions::ListInitExpression ^ ListInit(System::Linq::Expressions::NewExpression ^ newExpression, ... cli::array <System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, params System.Linq.Expressions.Expression[] initializers);
static member ListInit : System.Linq.Expressions.NewExpression * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.ListInitExpression
Public Shared Function ListInit (newExpression As NewExpression, ParamArray initializers As Expression()) As ListInitExpression

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

initializers
Expression[]

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

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

There is no instance method named "Add" (case insensitive) declared in newExpression.Type or its base type.

-or-

The add method on newExpression.Type or its base type does not take exactly one argument.

-or-

The type represented by the Type property of the first element of initializers is not assignable to the argument type of the add method on newExpression.Type or its base type.

-or-

More than one argument-compatible method named "Add" (case-insensitive) exists on newExpression.Type and/or its base type.

Remarks

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

In order to use this overload of ListInit(NewExpression, Expression[]), newExpression.Type or its base type must declare a single method named "Add" (case insensitive) that takes exactly one argument. The type of the argument must be assignable from the type represented by the Type property of the first element of initializers.

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 represents the add method that was discovered on newExpression.Type or its base type.

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

Applies to

ListInit(NewExpression, MethodInfo, IEnumerable<Expression>)

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

public:
 static System::Linq::Expressions::ListInitExpression ^ ListInit(System::Linq::Expressions::NewExpression ^ newExpression, System::Reflection::MethodInfo ^ addMethod, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo addMethod, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo? addMethod, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> initializers);
static member ListInit : System.Linq.Expressions.NewExpression * System.Reflection.MethodInfo * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.ListInitExpression
Public Shared Function ListInit (newExpression As NewExpression, addMethod As MethodInfo, initializers As IEnumerable(Of Expression)) As ListInitExpression

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

addMethod
MethodInfo

A MethodInfo that represents an instance method named "Add" (case insensitive), that adds an element to a collection.

initializers
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to use to populate the Initializers collection.

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

-or-

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

-or-

addMethod is not null 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.

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

Applies to

ListInit(NewExpression, MethodInfo, Expression[])

Source:
ListInitExpression.cs
Source:
ListInitExpression.cs
Source:
ListInitExpression.cs

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

public:
 static System::Linq::Expressions::ListInitExpression ^ ListInit(System::Linq::Expressions::NewExpression ^ newExpression, System::Reflection::MethodInfo ^ addMethod, ... cli::array <System::Linq::Expressions::Expression ^> ^ initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo addMethod, params System.Linq.Expressions.Expression[] initializers);
public static System.Linq.Expressions.ListInitExpression ListInit (System.Linq.Expressions.NewExpression newExpression, System.Reflection.MethodInfo? addMethod, params System.Linq.Expressions.Expression[] initializers);
static member ListInit : System.Linq.Expressions.NewExpression * System.Reflection.MethodInfo * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.ListInitExpression
Public Shared Function ListInit (newExpression As NewExpression, addMethod As MethodInfo, ParamArray initializers As Expression()) As ListInitExpression

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

addMethod
MethodInfo

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

initializers
Expression[]

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

Returns

A ListInitExpression that has the NodeType property equal to ListInit and the NewExpression property set to the specified value.

Exceptions

newExpression or initializers is null.

-or-

One or more elements of initializers are null.

newExpression.Type does not implement IEnumerable.

-or-

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

-or-

addMethod is not null 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.

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

Applies to