Expression.MemberInit Expression.MemberInit Expression.MemberInit Method

定義

新しいオブジェクトを作成し、そのオブジェクトのプロパティを初期化する式を表します。Represents an expression that creates a new object and initializes a property of the object.

オーバーロード

MemberInit(NewExpression, IEnumerable<MemberBinding>) MemberInit(NewExpression, IEnumerable<MemberBinding>) MemberInit(NewExpression, IEnumerable<MemberBinding>)

新しいオブジェクトを作成し、そのオブジェクトのプロパティを初期化する式を表します。Represents an expression that creates a new object and initializes a property of the object.

MemberInit(NewExpression, MemberBinding[]) MemberInit(NewExpression, MemberBinding[]) MemberInit(NewExpression, MemberBinding[])

MemberInitExpression を作成します。Creates a MemberInitExpression.

MemberInit(NewExpression, IEnumerable<MemberBinding>) MemberInit(NewExpression, IEnumerable<MemberBinding>) MemberInit(NewExpression, IEnumerable<MemberBinding>)

新しいオブジェクトを作成し、そのオブジェクトのプロパティを初期化する式を表します。Represents an expression that creates a new object and initializes a property of the object.

public:
 static System::Linq::Expressions::MemberInitExpression ^ MemberInit(System::Linq::Expressions::NewExpression ^ newExpression, System::Collections::Generic::IEnumerable<System::Linq::Expressions::MemberBinding ^> ^ bindings);
public static System.Linq.Expressions.MemberInitExpression MemberInit (System.Linq.Expressions.NewExpression newExpression, System.Collections.Generic.IEnumerable<System.Linq.Expressions.MemberBinding> bindings);
static member MemberInit : System.Linq.Expressions.NewExpression * seq<System.Linq.Expressions.MemberBinding> -> System.Linq.Expressions.MemberInitExpression

パラメーター

newExpression
NewExpression NewExpression NewExpression

NewExpression プロパティを等しく設定する NewExpressionA NewExpression to set the NewExpression property equal to.

bindings
IEnumerable<MemberBinding>

IEnumerable<T> コレクションへの読み込みに使用する MemberBinding オブジェクトを含む BindingsAn IEnumerable<T> that contains MemberBinding objects to use to populate the Bindings collection.

戻り値

MemberInitExpression と等しい NodeType プロパティと、指定した値に設定された MemberInit プロパティおよび NewExpression プロパティを含む BindingsA MemberInitExpression that has the NodeType property equal to MemberInit and the NewExpression and Bindings properties set to the specified values.

例外

newExpression または bindingsnull です。newExpression or bindings is null.

bindings の要素の Member プロパティは、newExpression.Type が表す型のメンバーを表しません。The Member property of an element of bindings does not represent a member of the type that newExpression.Type represents.

次の例では、新しいオブジェクトを作成し、オブジェクトのプロパティを初期化する式を示します。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:
// 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
' 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()()
    Console.WriteLine(test.Sample)
End Sub

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

注釈

TypeプロパティMemberInitExpressionと等しい、TypeプロパティのnewExpressionします。The Type property of the resulting MemberInitExpression is equal to the Type property of newExpression.

MemberInit(NewExpression, MemberBinding[]) MemberInit(NewExpression, MemberBinding[]) MemberInit(NewExpression, MemberBinding[])

MemberInitExpression を作成します。Creates a MemberInitExpression.

public:
 static System::Linq::Expressions::MemberInitExpression ^ MemberInit(System::Linq::Expressions::NewExpression ^ newExpression, ... cli::array <System::Linq::Expressions::MemberBinding ^> ^ bindings);
public static System.Linq.Expressions.MemberInitExpression MemberInit (System.Linq.Expressions.NewExpression newExpression, params System.Linq.Expressions.MemberBinding[] bindings);
static member MemberInit : System.Linq.Expressions.NewExpression * System.Linq.Expressions.MemberBinding[] -> System.Linq.Expressions.MemberInitExpression

パラメーター

newExpression
NewExpression NewExpression NewExpression

NewExpression プロパティを等しく設定する NewExpressionA NewExpression to set the NewExpression property equal to.

bindings
MemberBinding[]

MemberBinding コレクションへの読み込みに使用する Bindings オブジェクトの配列。An array of MemberBinding objects to use to populate the Bindings collection.

戻り値

MemberInitExpression と等しい NodeType プロパティと、指定した値に設定された MemberInit プロパティおよび NewExpression プロパティを含む BindingsA MemberInitExpression that has the NodeType property equal to MemberInit and the NewExpression and Bindings properties set to the specified values.

例外

newExpression または bindingsnull です。newExpression or bindings is null.

bindings の要素の Member プロパティは、newExpression.Type が表す型のメンバーを表しません。The Member property of an element of bindings does not represent a member of the type that newExpression.Type represents.

次の例を使用する方法を示します、MemberInit(NewExpression, MemberBinding[])を作成する方法、MemberInitExpression新しいオブジェクトの 2 つのメンバーの初期化を表します。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}
}
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

注釈

TypeプロパティMemberInitExpressionと等しい、TypeプロパティのnewExpressionします。The Type property of the resulting MemberInitExpression is equal to the Type property of newExpression.

適用対象