Expression.ElementInit Method

Definition

Creates an ElementInit.

Overloads

ElementInit(MethodInfo, IEnumerable<Expression>)

Creates an ElementInit, given an IEnumerable<T> as the second argument.

ElementInit(MethodInfo, Expression[])

Creates an ElementInit, given an array of values as the second argument.

ElementInit(MethodInfo, IEnumerable<Expression>)

Source:
ElementInit.cs
Source:
ElementInit.cs
Source:
ElementInit.cs

Creates an ElementInit, given an IEnumerable<T> as the second argument.

C#
public static System.Linq.Expressions.ElementInit ElementInit(System.Reflection.MethodInfo addMethod, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);

Parameters

addMethod
MethodInfo

A MethodInfo to set the AddMethod property equal to.

arguments
IEnumerable<Expression>

An IEnumerable<T> that contains Expression objects to set the Arguments property equal to.

Returns

An ElementInit that has the AddMethod and Arguments properties set to the specified values.

Exceptions

addMethod or arguments is null.

The method that addMethod represents is not named "Add" (case insensitive).

-or-

The method that addMethod represents is not an instance method.

-or-

arguments does not contain the same number of elements as the number of parameters for the method that addMethod represents.

-or-

The Type property of one or more elements of arguments is not assignable to the type of the corresponding parameter of the method that addMethod represents.

Examples

The following example demonstrates how to use the ElementInit(MethodInfo, Expression[]) method to create an ElementInit that represents calling the Add method to initialize an element of a dictionary collection.

C#
string tree = "maple";

System.Reflection.MethodInfo addMethod = typeof(Dictionary<int, string>).GetMethod("Add");

// Create an ElementInit that represents calling
// Dictionary<int, string>.Add(tree.Length, tree).
System.Linq.Expressions.ElementInit elementInit =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree.Length),
        System.Linq.Expressions.Expression.Constant(tree));

Console.WriteLine(elementInit.ToString());

// This code produces the following output:
//
// Void Add(Int32, System.String)(5,"maple")

Remarks

The addMethod parameter must represent an instance method named "Add" (case insensitive). The add method must have the same number of parameters as the number of elements in arguments. The Type property of each element in arguments must be assignable to the type of the corresponding parameter of the add method, possibly after quoting.

Note

An element will be quoted only if the corresponding method parameter is of type Expression. Quoting means the element is wrapped in a Quote node. The resulting node is a UnaryExpression whose Operand property is the element of arguments.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ElementInit(MethodInfo, Expression[])

Source:
ElementInit.cs
Source:
ElementInit.cs
Source:
ElementInit.cs

Creates an ElementInit, given an array of values as the second argument.

C#
public static System.Linq.Expressions.ElementInit ElementInit(System.Reflection.MethodInfo addMethod, params System.Linq.Expressions.Expression[] arguments);

Parameters

addMethod
MethodInfo

A MethodInfo to set the AddMethod property equal to.

arguments
Expression[]

An array of Expression objects to set the Arguments property equal to.

Returns

An ElementInit that has the AddMethod and Arguments properties set to the specified values.

Exceptions

addMethod or arguments is null.

The method that addMethod represents is not named "Add" (case insensitive).

-or-

The method that addMethod represents is not an instance method.

-or-

arguments does not contain the same number of elements as the number of parameters for the method that addMethod represents.

-or-

The Type property of one or more elements of arguments is not assignable to the type of the corresponding parameter of the method that addMethod represents.

Examples

The following example demonstrates how to use the ElementInit(MethodInfo, Expression[]) method to create an ElementInit that represents calling the Add method to initialize an element of a dictionary collection.

C#
string tree = "maple";

System.Reflection.MethodInfo addMethod = typeof(Dictionary<int, string>).GetMethod("Add");

// Create an ElementInit that represents calling
// Dictionary<int, string>.Add(tree.Length, tree).
System.Linq.Expressions.ElementInit elementInit =
    System.Linq.Expressions.Expression.ElementInit(
        addMethod,
        System.Linq.Expressions.Expression.Constant(tree.Length),
        System.Linq.Expressions.Expression.Constant(tree));

Console.WriteLine(elementInit.ToString());

// This code produces the following output:
//
// Void Add(Int32, System.String)(5,"maple")

Remarks

The addMethod parameter must represent an instance method named "Add" (case insensitive). The add method must have the same number of parameters as the number of elements in arguments. The Type property of each element in arguments must be assignable to the type of the corresponding parameter of the add method, possibly after quoting.

Note

An element will be quoted only if the corresponding method parameter is of type Expression. Quoting means the element is wrapped in a Quote node. The resulting node is a UnaryExpression whose Operand property is the element of arguments.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0