MemberInit Method (NewExpression, IEnumerable(MemberBinding))
Collapse the table of content
Expand the table of content

Expression.MemberInit Method (NewExpression, IEnumerable(Of MemberBinding))

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Represents an expression that creates a new object and initializes a property of the object.

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

'Declaration
Public Shared Function MemberInit ( _
	newExpression As NewExpression, _
	bindings As IEnumerable(Of MemberBinding) _
) As MemberInitExpression

Parameters

newExpression
Type: System.Linq.Expressions.NewExpression
A NewExpression to set the NewExpression property equal to.
bindings
Type: System.Collections.Generic.IEnumerable(Of MemberBinding)
An IEnumerable(Of T) that contains 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.

ExceptionCondition
ArgumentNullException

newExpression or bindings is Nothing.

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 an expression that creates a new object and initializes a property of the object.


' Add the following directive to your file:
' Imports System.Linq.Expressions  

Class TestMemberInitClass
    Public Property Sample As Integer
End Class

Sub MemberInit()
    ' This expression creates a new TestMemberInitClass object
    ' and assigns 10 to its Sample property.
    Dim testExpr As Expression = Expression.MemberInit(
        Expression.[New](GetType(TestMemberInitClass)),
        New List(Of MemberBinding)() From {
            Expression.Bind(GetType(TestMemberInitClass).GetMember("Sample")(0), Expression.Constant(10))
        }
    )

    ' The following statement first creates an expression tree,
    ' then compiles it, and then runs it.
    Dim test = Expression.Lambda(Of Func(Of TestMemberInitClass))(testExpr).Compile()()
    outputBlock.Text &= test.Sample & vbCrLf
End Sub

' This code example produces the following output:
'
' 10


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft