Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Expression.ElementInit Method (MethodInfo, IEnumerable<Expression>)

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

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)
public static ElementInit ElementInit(
	MethodInfo addMethod,
	IEnumerable<Expression> arguments
)

Parameters

addMethod
Type: System.Reflection.MethodInfo

A MethodInfo to set the AddMethod property equal to.

arguments
Type: System.Collections.Generic.IEnumerable<Expression>

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

Return Value

Type: System.Linq.Expressions.ElementInit
An ElementInit that has the AddMethod and Arguments properties set to the specified values.
ExceptionCondition
ArgumentNullException

addMethod or arguments is null.

ArgumentException

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.

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.

NoteNote

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.

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.

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

.NET Framework

Supported in: 4.5.1, 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 8, Silverlight 8.1

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.