Compartir a través de


Expression.Property Method (Expression, String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a MemberExpression that represents accessing a property.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function Property ( _
    expression As Expression, _
    propertyName As String _
) As MemberExpression
public static MemberExpression Property(
    Expression expression,
    string propertyName
)

Parameters

  • propertyName
    Type: System.String
    The name of a property to be accessed.

Return Value

Type: System.Linq.Expressions.MemberExpression
A MemberExpression that has the NodeType property equal to MemberAccess, the Expression property set to expression, and the Member property set to the PropertyInfo that represents the property denoted by propertyName.

Exceptions

Exception Condition
ArgumentNullException

expression or propertyName is nulla null reference (Nothing in Visual Basic).

ArgumentException

No property named propertyName is defined in expression.Type or its base types.

Remarks

The Type property of the resulting MemberExpression is equal to the PropertyType property of the PropertyInfo that represents the property denoted by propertyName.

This method searches expression.Type and its base types for a property that has the name propertyName. Public properties are given preference over non-public properties. If a matching property is found, this method passes expression and the PropertyInfo that represents that property to Property.

Examples

The following example shows how to create an expression that represents accessing a property.

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

Class TestPropertyClass
    Public Property Sample As Integer
End Class

Sub TestProperty()

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

    ' This expression represents accessing a property.
    ' For static properties, the first parameter must be Nothing.
    Dim propertyExpr As Expression = Expression.Property(
          Expression.Constant(obj),
          "sample"
      )

    ' The following statement first creates an expression tree,
    ' then compiles it, and then runs it.
    outputBlock.Text &= Expression.Lambda(Of Func(Of Integer))(propertyExpr).Compile()() & vbCrLf
End Sub

' This code example produces the following output:
'
' 40
    // Add the following directive to your file:
    // using System.Linq.Expressions;  

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

    static void TestProperty(System.Windows.Controls.TextBlock outputBlock)
    {
       TestPropertyClass obj = new TestPropertyClass();
       obj.sample = 40;

       // This expression represents accessing a property.
       // For static fields, the first parameter must be null.
       Expression propertyExpr = Expression.Property(
           Expression.Constant(obj),
           "sample"
       );

       // The following statement first creates an expression tree,
       // then compiles it, and then runs it.
       outputBlock.Text += Expression.Lambda<Func<int>>(propertyExpr).Compile()() + "\n";
    }

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.