Expression.Field 方法

定義

建立代表存取欄位的 MemberExpressionCreates a MemberExpression that represents accessing a field.

多載

Field(Expression, FieldInfo)

建立代表存取欄位的 MemberExpressionCreates a MemberExpression that represents accessing a field.

Field(Expression, String)

建立 MemberExpression,代表存取可指定欄位名稱的欄位。Creates a MemberExpression that represents accessing a field given the name of the field.

Field(Expression, Type, String)

建立代表存取欄位的 MemberExpressionCreates a MemberExpression that represents accessing a field.

Field(Expression, FieldInfo)

建立代表存取欄位的 MemberExpressionCreates a MemberExpression that represents accessing a 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);
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 屬性設定為與之相等的 ExpressionAn Expression to set the Expression property equal to. 若為 static (在 Visual Basic 中為 Shared),expression 必須是 nullFor static (Shared in Visual Basic), expression must be null.

field
FieldInfo

要將 FieldInfo 屬性設定為與之相等的 MemberThe FieldInfo to set the Member property equal to.

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccess,且 ExpressionMember 屬性設定為指定的值。A MemberExpression that has the NodeType property equal to MemberAccess and the Expression and Member properties set to the specified values.

例外狀況

fieldnullfield is null.

-或--or- field 所表示的欄位不是 static (在 Visual Basic 中為 Shared),且 expressionnullThe field represented by field is not static (Shared in Visual Basic) and expression is null.

expression.Type 無法指派給 field 所代表之欄位的宣告類型。expression.Type is not assignable to the declaring type of the field represented by field.

備註

Type產生的屬性等於的 MemberExpression FieldType 屬性 fieldThe Type property of the resulting MemberExpression is equal to the FieldType property of field.

適用於

Field(Expression, String)

建立 MemberExpression,代表存取可指定欄位名稱的欄位。Creates a MemberExpression that represents accessing a field given the name of the field.

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);
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,其 Type 包含名為 fieldName 的欄位。An Expression whose Type contains a field named fieldName. 如果是靜態欄位,可以是 Null。This can be null for static fields.

fieldName
String

要存取的欄位名稱。The name of a field to be accessed.

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccessExpression 屬性設定為 expression,且 Member 屬性設定為 FieldInfo,代表 fieldName 所表示的欄位。A MemberExpression that has the NodeType property equal to MemberAccess, the Expression property set to expression, and the Member property set to the FieldInfo that represents the field denoted by fieldName.

例外狀況

expressionfieldNamenullexpression or fieldName is null.

fieldName.Type 或其基底類型中沒有定義名為 expression 的欄位。No field named fieldName is defined in expression.Type or its base types.

範例

下列程式碼範例示範如何建立代表存取欄位的運算式。The following code example shows how to create an expression that represents accessing a field.

// 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 屬性,其代表所表示 FieldInfo 的欄位 fieldNameThe Type property of the resulting MemberExpression is equal to the FieldType property of the FieldInfo that represents the field denoted by fieldName.

這個方法會搜尋 expression 。具有名稱之欄位的類型及其基底類型 fieldNameThis method searches expression.Type and its base types for a field that has the name fieldName. 公用欄位是針對非公用欄位提供喜好設定。Public fields are given preference over non-public fields. 如果找到相符的欄位,這個方法會將 expression FieldInfo 表示該欄位的傳遞給 FieldIf a matching field is found, this method passes expression and the FieldInfo that represents that field to Field.

適用於

Field(Expression, Type, String)

建立代表存取欄位的 MemberExpressionCreates a MemberExpression that represents accessing a field.

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

欄位的包含物件。The containing object of the field. 如果是靜態欄位,可以是 Null。This can be null for static fields.

type
Type

包含欄位的 TypeThe Type that contains the field.

fieldName
String

要存取的欄位。The field to be accessed.

傳回

MemberExpression

建立的 MemberExpressionThe created MemberExpression.

適用於