Expression.MemberInit 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示建立新物件並初始化物件屬性的運算式。
多載
MemberInit(NewExpression, IEnumerable<MemberBinding>) |
表示建立新物件並初始化物件屬性的運算式。 |
MemberInit(NewExpression, MemberBinding[]) |
MemberInit(NewExpression, IEnumerable<MemberBinding>)
表示建立新物件並初始化物件屬性的運算式。
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
Public Shared Function MemberInit (newExpression As NewExpression, bindings As IEnumerable(Of MemberBinding)) As MemberInitExpression
參數
- newExpression
- NewExpression
要將 NewExpression 屬性設定為與之相等的 NewExpression。
- bindings
- IEnumerable<MemberBinding>
IEnumerable<T>,其中包含用以填入 MemberBinding 集合的 Bindings 物件。
傳回
MemberInitExpression,其 NodeType 屬性等於 MemberInit,且 NewExpression 和 Bindings 屬性設定為指定的值。
例外狀況
newExpression
或 bindings
為 null
。
bindings
之項目的 Member 屬性不表示 newExpression
.Type 所表示之類型的成員。
範例
下列範例示範建立新物件的表達式,並初始化 對象的屬性。
// 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
備註
產生的 TypeMemberInitExpression 屬性等於 Type 的 newExpression
屬性。
適用於
MemberInit(NewExpression, MemberBinding[])
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
Public Shared Function MemberInit (newExpression As NewExpression, ParamArray bindings As MemberBinding()) As MemberInitExpression
參數
- newExpression
- NewExpression
要將 NewExpression 屬性設定為與之相等的 NewExpression。
- bindings
- MemberBinding[]
用以填入 MemberBinding 集合之 Bindings 物件的陣列。
傳回
MemberInitExpression,其 NodeType 屬性等於 MemberInit,且 NewExpression 和 Bindings 屬性設定為指定的值。
例外狀況
newExpression
或 bindings
為 null
。
bindings
之項目的 Member 屬性不表示 newExpression
.Type 所表示之類型的成員。
範例
下列範例示範如何使用 MemberInit(NewExpression, MemberBinding[]) 方法來建立 , MemberInitExpression 代表新物件兩個成員的初始化。
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
備註
產生的 TypeMemberInitExpression 屬性等於 Type 的 newExpression
屬性。