Aracılığıyla paylaş


Expression.PropertyOrField(Expression, String) Yöntem

Tanım

Bir MemberExpression özelliğe veya alana erişimi temsil eden bir oluşturur.

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);
[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 PropertyOrField(System.Linq.Expressions.Expression expression, string propertyOrFieldName);
static member PropertyOrField : 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 PropertyOrField : System.Linq.Expressions.Expression * string -> System.Linq.Expressions.MemberExpression
Public Shared Function PropertyOrField (expression As Expression, propertyOrFieldName As String) As MemberExpression

Parametreler

expression
Expression

Expression Type adlı propertyOrFieldNamebir özellik veya alan içeren bir.

propertyOrFieldName
String

Erişilecek özellik veya alanın adı.

Döndürülenler

özelliğine MemberAccesseşit olanNodeType, Expression özelliği olarak expressionayarlanmış, Member özelliği ise ile belirtilen propertyOrFieldNameözellik veya alanı temsil eden veya FieldInfo olarak ayarlanmıştırPropertyInfo.MemberExpression

Öznitelikler

Özel durumlar

expression veya propertyOrFieldName şeklindedir null.

içinde expressionadlı propertyOrFieldName bir özellik veya alan tanımlanmadı. Tür veya temel türleri.

Örnekler

Aşağıdaki örnekte, bir özelliğe veya alana erişimi temsil eden bir ifadenin nasıl oluşturulacağı gösterilmektedir.

// 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

Açıklamalar

Sonuçta TypeMemberExpression elde edilen özelliği, veya ile belirtilen propertyOrFieldNameözelliği veya FieldType alanı temsil eden veya özelliklerine FieldInfoPropertyInfo eşittirPropertyType.

Bu yöntem arar expression. adına propertyOrFieldNamesahip bir örnek özelliği veya alanı için türü ve temel türleri. Statik özellikler veya alanlar desteklenmez. Ortak özellikler ve alanlar, genel olmayan özelliklere ve alanlara göre tercih edilir. Ayrıca, özelliklere alanlar üzerinde tercih verilir. Eşleşen bir özellik veya alan bulunursa, bu yöntem ve PropertyInfoFieldInfo bu özelliği veya alanı Property sırasıyla veya Fieldöğesine geçirirexpression.

Şunlara uygulanır