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

Expression.MemberInit Method (NewExpression, MemberBinding[])

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)
public static MemberInitExpression MemberInit(
	NewExpression newExpression,
	params MemberBinding[] bindings
)

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 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 string Species {get; set;}
    public int Age {get; set;}
}

public static void CreateMemberInitExpression()
{
    System.Linq.Expressions.NewExpression newAnimal =
        System.Linq.Expressions.Expression.New(typeof(Animal));

    System.Reflection.MemberInfo speciesMember =
        typeof(Animal).GetMember("Species")[0];
    System.Reflection.MemberInfo ageMember =
        typeof(Animal).GetMember("Age")[0];

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

    // Create a MemberInitExpression that represents initializing 
    // two members of the 'Animal' class.
    System.Linq.Expressions.MemberInitExpression 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}
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.