This topic has not yet been rated - Rate this topic

Expression.Property Method (Expression, String)

Creates a MemberExpression that represents accessing a property.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)
public static MemberExpression Property(
	Expression expression,
	string propertyName
)

Parameters

expression
Type: System.Linq.Expressions.Expression

An Expression whose Type contains a property named propertyName. This can be null for static properties.

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.
ExceptionCondition
ArgumentNullException

expression or propertyName is null.

ArgumentException

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

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.

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

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

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

         static void TestProperty()
         {
             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.
             Console.WriteLine(Expression.Lambda<Func<int>>(propertyExpr).Compile()());            
         }

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.