Export (0) Print
Expand All

Expression.MemberInit Method (NewExpression, MemberBinding())

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

'Declaration
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.

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

.NET Framework

Supported in: 4.6, 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

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

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.

Show:
© 2014 Microsoft