Condividi tramite


Expression.MemberInit Method (NewExpression, array<MemberBinding[])

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

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

Syntax

'Declaration
Public Shared Function MemberInit ( _
    newExpression As NewExpression, _
    ParamArray bindings As MemberBinding() _
) As MemberInitExpression
public static MemberInitExpression MemberInit(
    NewExpression newExpression,
    params 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 how to use the MemberInit(NewExpression, array<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("SilverlightApplication.MemberInitExample+Animal"))
   Dim speciesMember As System.Reflection.MemberInfo = _
       Type.GetType("SilverlightApplication.MemberInitExample+Animal").GetMember("Species")(0)
   Dim ageMember As System.Reflection.MemberInfo = _
       Type.GetType("SilverlightApplication.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)

   outputBlock.Text &= memberInitExpression.ToString() & vbCrLf

   ' This code produces the following output:
   '
   ' new Animal() {Species = "horse", Age = 12}
End Sub
class Animal
{
   public string Species { get; set; }
   public int Age { get; set; }
}

public static void CreateMemberInitExpression(System.Windows.Controls.TextBlock outputBlock)
{
   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);

   outputBlock.Text += memberInitExpression.ToString() + "\n";

   // This code produces the following output:
   //
   // new Animal() {Species = "horse", Age = 12}
}

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.