Expression.MemberInit Method (NewExpression, MemberBinding())

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Public Shared Function MemberInit (
	newExpression As NewExpression,
	ParamArray bindings As MemberBinding()
) As MemberInitExpression

Parameters

newExpression
Type: System.Linq.Expressions.NewExpression

A NewExpression to set the NewExpression property equal to.

bindings
Type: System.Linq.Expressions.MemberBinding()

An array of MemberBinding objects to use to populate the Bindings collection.

Return Value

Type: System.Linq.Expressions.MemberInitExpression

A MemberInitExpression that has the NodeType property equal to MemberInit and the NewExpression and Bindings properties set to the specified values.

Exception Condition
ArgumentNullException

newExpression or bindings is null.

ArgumentException

The Member property of an element of bindings does not represent a member of the type that newExpression.Type represents.

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

The following example demonstrates how to use the MemberInit(NewExpression, MemberBinding()) method to create a MemberInitExpression that represents the initialization of two members of a new object.

Class Animal
    Public Species As String
    Public Age As Integer
End Class

Shared Sub CreateMemberInitExpression()
    Dim newAnimal As System.Linq.Expressions.NewExpression = _
        System.Linq.Expressions.Expression.[New](Type.GetType("ExpressionVB.MemberInitExample+Animal"))

    Dim speciesMember As System.Reflection.MemberInfo = _
        Type.GetType("ExpressionVB.MemberInitExample+Animal").GetMember("Species")(0)
    Dim ageMember As System.Reflection.MemberInfo = _
        Type.GetType("ExpressionVB.MemberInitExample+Animal").GetMember("Age")(0)

    ' Create a MemberBinding object for each member
    ' that you want to initialize.
    Dim speciesMemberBinding As System.Linq.Expressions.MemberBinding = _
        System.Linq.Expressions.Expression.Bind( _
            speciesMember, _
            System.Linq.Expressions.Expression.Constant("horse"))
    Dim ageMemberBinding As System.Linq.Expressions.MemberBinding = _
        System.Linq.Expressions.Expression.Bind( _
            ageMember, _
            System.Linq.Expressions.Expression.Constant(12))

    ' Create a MemberInitExpression that represents initializing
    ' two members of the 'Animal' class.
    Dim memberInitExpression As System.Linq.Expressions.MemberInitExpression = _
        System.Linq.Expressions.Expression.MemberInit( _
            newAnimal, _
            speciesMemberBinding, _
            ageMemberBinding)

    Console.WriteLine(memberInitExpression.ToString())

    ' This code produces the following output:
    '
    ' new Animal() {Species = "horse", Age = 12}
End Sub

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: