Expression.PropertyOrField(Expression, String) 方法

定義

建立 MemberExpression,代表存取屬性或欄位。Creates a MemberExpression that represents accessing a property or field.

public:
 static System::Linq::Expressions::MemberExpression ^ PropertyOrField(System::Linq::Expressions::Expression ^ expression, System::String ^ propertyOrFieldName);
public static System.Linq.Expressions.MemberExpression PropertyOrField (System.Linq.Expressions.Expression expression, string propertyOrFieldName);
static member PropertyOrField : System.Linq.Expressions.Expression * string -> System.Linq.Expressions.MemberExpression
Public Shared Function PropertyOrField (expression As Expression, propertyOrFieldName As String) As MemberExpression

參數

expression
Expression

Expression,其 Type 包含名為 propertyOrFieldName 的屬性或欄位。An Expression whose Type contains a property or field named propertyOrFieldName.

propertyOrFieldName
String

要存取之屬性或欄位的名稱。The name of a property or field to be accessed.

傳回

MemberExpression

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

例外狀況

expressionpropertyOrFieldNamenullexpression or propertyOrFieldName is null.

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

範例

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

// Add the following directive to your file:
// using System.Linq.Expressions;

class TestClass
{
    public int sample { get; set; }
}

static void TestPropertyOrField()
{
    TestClass obj = new TestClass();
    obj.sample = 40;

    // This expression represents accessing a property or field.
    // For static properties or fields, the first parameter must be null.
    Expression memberExpr = Expression.PropertyOrField(
        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>>(memberExpr).Compile()());
}

// This code example produces the following output:
//
// 40

' Add the following directive to your file:
' Imports System.Linq.Expressions  

Class TestClass
    Public Property Sample As Integer
End Class

Sub TestPropertyOrField()

    Dim obj As New TestClass()
    obj.Sample = 40

    ' This expression represents accessing a property or field.
    ' For static properties or fields, the first parameter must be Nothing.
    Dim memberExpr As Expression = Expression.PropertyOrField(
          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))(memberExpr).Compile()())
End Sub

' This code example produces the following output:
'
' 40

備註

Type產生的屬性 MemberExpressionPropertyType 分別等於或的或屬性,代表所表示 FieldType PropertyInfo FieldInfo 的屬性或欄位 propertyOrFieldNameThe Type property of the resulting MemberExpression is equal to the PropertyType or FieldType properties of the PropertyInfo or FieldInfo, respectively, that represents the property or field denoted by propertyOrFieldName.

這個方法會搜尋 expression 。類型及其基底類型,適用于具有名稱的實例屬性或欄位 propertyOrFieldNameThis method searches expression.Type and its base types for an instance property or field that has the name propertyOrFieldName. 不支援靜態屬性或欄位。Static properties or fields are not supported. 公用屬性和欄位會優先考慮非公用屬性和欄位。Public properties and fields are given preference over non-public properties and fields. 此外,屬性也會優先考慮欄位。Also, properties are given preference over fields. 如果找到相符的屬性或欄位,這個方法會將 expression PropertyInfoFieldInfo 分別代表該屬性或欄位的或傳遞給或 Property FieldIf a matching property or field is found, this method passes expression and the PropertyInfo or FieldInfo that represents that property or field to Property or Field, respectively.

適用於