Expression.MemberInit Method

Definition

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

Overloads

MemberInit(NewExpression, IEnumerable<MemberBinding>)

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

MemberInit(NewExpression, MemberBinding[])

Creates a MemberInitExpression.

MemberInit(NewExpression, IEnumerable<MemberBinding>)

Source:
MemberInitExpression.cs
Source:
MemberInitExpression.cs
Source:
MemberInitExpression.cs

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

C#
public static System.Linq.Expressions.MemberInitExpression MemberInit(System.Linq.Expressions.NewExpression newExpression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.MemberBinding> bindings);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

bindings
IEnumerable<MemberBinding>

An IEnumerable<T> that contains MemberBinding objects to use to populate the Bindings collection.

Returns

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

Exceptions

newExpression or bindings is null.

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

Examples

The following example demonstrates an expression that creates a new object and initializes a property of the object.

C#

// Add the following directive to your file:
// using System.Linq.Expressions;

class TestMemberInitClass
{
    public int sample { get; set; }
}

static void MemberInit()
{
    // This expression creates a new TestMemberInitClass object
    // and assigns 10 to its sample property.
    Expression testExpr = Expression.MemberInit(
        Expression.New(typeof(TestMemberInitClass)),
        new List<MemberBinding>() {
            Expression.Bind(typeof(TestMemberInitClass).GetMember("sample")[0], Expression.Constant(10))
        }
    );

    // The following statement first creates an expression tree,
    // then compiles it, and then runs it.
    var test = Expression.Lambda<Func<TestMemberInitClass>>(testExpr).Compile()();
    Console.WriteLine(test.sample);
}

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

Remarks

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

MemberInit(NewExpression, MemberBinding[])

Source:
MemberInitExpression.cs
Source:
MemberInitExpression.cs
Source:
MemberInitExpression.cs
C#
public static System.Linq.Expressions.MemberInitExpression MemberInit(System.Linq.Expressions.NewExpression newExpression, params System.Linq.Expressions.MemberBinding[] bindings);

Parameters

newExpression
NewExpression

A NewExpression to set the NewExpression property equal to.

bindings
MemberBinding[]

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

Returns

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

Exceptions

newExpression or bindings is null.

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

Examples

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.

C#
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}
}

Remarks

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0