Expression.Field 方法

定義

建立代表存取欄位的 MemberExpression

多載

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 屬性設定為與之相等的 Expression。 若為 static (在 Visual Basic 中為 Shared),expression 必須是 null

field
FieldInfo

要將 FieldInfo 屬性設定為與之相等的 Member

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccess,且 ExpressionMember 屬性設定為指定的值。

例外狀況

fieldnull

-或- field 所表示的欄位不是 static (在 Visual Basic 中為 Shared),且 expressionnull

expression.Type 無法指派給 field 所代表之欄位的宣告類型。

備註

產生的 Type MemberExpression 屬性等於 FieldTypefield 屬性。

適用於

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);
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 的欄位。 如果是靜態欄位,可以是 Null。

fieldName
String

要存取的欄位名稱。

傳回

MemberExpression

MemberExpression,其 NodeType 屬性等於 MemberAccessExpression 屬性設定為 expression,且 Member 屬性設定為 FieldInfo,代表 fieldName 所表示的欄位。

例外狀況

expressionfieldNamenull

fieldName.Type 或其基底類型中沒有定義名為 expression 的欄位。

範例

下列程式碼範例示範如何建立代表存取欄位的運算式。

// 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 ,代表 所 fieldName 表示的欄位。

這個方法會搜尋 expression 。型別和其基底類型,其名稱為 的 fieldName 欄位。 公用欄位的喜好設定高於非公用欄位。 如果找到相符的欄位,這個方法會將 代表該欄位的 傳遞 expression FieldInfoField

適用於

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

欄位的包含物件。 如果是靜態欄位,可以是 Null。

type
Type

包含欄位的 Type

fieldName
String

要存取的欄位。

傳回

MemberExpression

建立的 MemberExpression

適用於