共用方式為


Expression.MemberInit Method (NewExpression, IEnumerable<MemberBinding>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

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

Parameters

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.

Exceptions

Exception Condition
ArgumentNullException

newExpression or bindings is nulla null reference (Nothing in Visual Basic).

ArgumentException

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

Remarks

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

Examples

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

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

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

static void MemberInit(System.Windows.Controls.TextBlock outputBlock)
{
   // 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()();
   outputBlock.Text += test.sample + "\n";
}

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.