Expression.Field 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立 MemberExpression 一個代表存取欄位的 。
多載
| 名稱 | Description |
|---|---|
| Field(Expression, FieldInfo) |
建立 MemberExpression 一個代表存取欄位的 。 |
| Field(Expression, String) |
建立 MemberExpression 一個代表以欄位名稱存取該欄位的行為。 |
| Field(Expression, Type, String) |
建立 MemberExpression 一個代表存取欄位的 。 |
Field(Expression, FieldInfo)
建立 MemberExpression 一個代表存取欄位的 。
public:
static System::Linq::Expressions::MemberExpression ^ Field(System::Linq::Expressions::Expression ^ expression, System::Reflection::FieldInfo ^ field);
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression expression, System.Reflection.FieldInfo field);
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression? expression, System.Reflection.FieldInfo field);
static member Field : System.Linq.Expressions.Expression * System.Reflection.FieldInfo -> System.Linq.Expressions.MemberExpression
Public Shared Function Field (expression As Expression, field As FieldInfo) As MemberExpression
參數
- expression
- Expression
Expression A 將屬性Expression設為 。 對於 static (Shared 在 Visual Basic 中), expression 必須是 null。
傳回
MemberExpression一個屬性等NodeType於 ,MemberAccess且 Expression 和 Member 屬性設定為指定值的 a。
例外狀況
field 為 null。
-或-
所表示的 field 欄位不是 static (Shared 在 Visual Basic 中),且 expression 是 null。
expression.類型無法指派到由 field表示的欄位的宣告類型。
備註
Type所得MemberExpression的性質等FieldType於 的field性質。
適用於
Field(Expression, String)
建立 MemberExpression 一個代表以欄位名稱存取該欄位的行為。
public:
static System::Linq::Expressions::MemberExpression ^ Field(System::Linq::Expressions::Expression ^ expression, System::String ^ fieldName);
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression expression, string fieldName);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed.")]
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression expression, string fieldName);
static member Field : System.Linq.Expressions.Expression * string -> System.Linq.Expressions.MemberExpression
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed.")>]
static member Field : System.Linq.Expressions.Expression * string -> System.Linq.Expressions.MemberExpression
Public Shared Function Field (expression As Expression, fieldName As String) As MemberExpression
參數
- expression
- Expression
一個 Expression whose Type 包含一個名為 fieldName的欄位。 靜態場則可為空。
- fieldName
- String
要存取的欄位名稱。
傳回
MemberExpression一個具有 NodeType 等於 的性質MemberAccess,且Expression性質設為 expression,且Member性質設於 ,FieldInfo代表由 表示fieldName的域。
- 屬性
例外狀況
expression 或 fieldName 為 null。
在 expression中沒有定義任何命名fieldName欄位。類型或其基礎類型。
範例
以下程式碼範例展示了如何建立一個表示式以表示存取欄位的表達式。
// Add the following directive to your file:
// using System.Linq.Expressions;
class TestFieldClass
{
int sample = 40;
}
static void TestField()
{
TestFieldClass obj = new TestFieldClass();
// This expression represents accessing a field.
// For static fields, the first parameter must be null.
Expression fieldExpr = Expression.Field(
Expression.Constant(obj),
"sample"
);
// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Console.WriteLine(Expression.Lambda<Func<int>>(fieldExpr).Compile()());
}
// This code example produces the following output:
//
// 40
' Add the following directive to your file:
' Imports System.Linq.Expressions
Class TestFieldClass
Dim sample As Integer = 40
End Class
Sub TestField()
Dim obj As New TestFieldClass()
' This expression represents accessing a field.
' For static fields, the first parameter must be Nothing.
Dim fieldExpr As Expression = Expression.Field(
Expression.Constant(obj),
"sample"
)
' The following statement first creates an expression tree,
' then compiles it, and then runs it.
Console.WriteLine(Expression.Lambda(Of Func(Of Integer))(fieldExpr).Compile()())
End Sub
' This code example produces the following output:
'
' 40
備註
Type所得的MemberExpression性質等於FieldType表示 的fieldName域 的FieldInfo性質。
此方法搜尋 expression。類型及其對一個名為 fieldName的欄位的基底類型。 公共領域優先於非公共領域。 若找到匹配的域,此方法將 與 該域 傳遞expressionFieldInfo為 Field。
適用於
Field(Expression, Type, String)
建立 MemberExpression 一個代表存取欄位的 。
public:
static System::Linq::Expressions::MemberExpression ^ Field(System::Linq::Expressions::Expression ^ expression, Type ^ type, System::String ^ fieldName);
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression expression, Type type, string fieldName);
public static System.Linq.Expressions.MemberExpression Field(System.Linq.Expressions.Expression? expression, Type type, string fieldName);
static member Field : System.Linq.Expressions.Expression * Type * string -> System.Linq.Expressions.MemberExpression
Public Shared Function Field (expression As Expression, type As Type, fieldName As String) As MemberExpression
參數
- expression
- Expression
場中包含的物件。 靜態場則可為空。
- fieldName
- String
待進入的欄位。
傳回
被創造 MemberExpression的。