Compartir a través de


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

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

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

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

Syntax

'Declaration
Public Shared Function ElementInit ( _
    addMethod As MethodInfo, _
    arguments As IEnumerable(Of Expression) _
) As ElementInit
public static ElementInit ElementInit(
    MethodInfo addMethod,
    IEnumerable<Expression> arguments
)

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

addMethod or arguments is nulla null reference (Nothing in Visual Basic).

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.

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.

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.

Examples

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

Dim tree As String = "maple"

Dim addMethod As System.Reflection.MethodInfo = _
    Type.GetType("System.Collections.Generic.Dictionary`2[System.Int32, System.String]").GetMethod("Add")

' Create an ElementInit that represents calling
' Dictionary(Of Integer, String).Add(tree.Length, tree).
Dim elementInit As System.Linq.Expressions.ElementInit = _
    System.Linq.Expressions.Expression.ElementInit( _
        addMethod, _
        System.Linq.Expressions.Expression.Constant(tree.Length), _
        System.Linq.Expressions.Expression.Constant(tree))

outputBlock.Text &= elementInit.ToString() & vbCrLf

' This code produces the following output:
'
' Void Add(Int32, System.String)(5,"maple")
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));

outputBlock.Text += elementInit.ToString() + "\n";

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

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.