Expression.Property Method

Definition

Creates a MemberExpression that represents accessing a property.

Overloads

Property(Expression, String, Expression[])

Creates an IndexExpression representing the access to an indexed property.

Property(Expression, PropertyInfo, Expression[])

Creates an IndexExpression representing the access to an indexed property.

Property(Expression, PropertyInfo, IEnumerable<Expression>)

Creates an IndexExpression representing the access to an indexed property.

Property(Expression, Type, String)

Creates a MemberExpression accessing a property.

Property(Expression, PropertyInfo)

Creates a MemberExpression that represents accessing a property.

Property(Expression, MethodInfo)

Creates a MemberExpression that represents accessing a property by using a property accessor method.

Property(Expression, String)

Creates a MemberExpression that represents accessing a property.

Property(Expression, String, Expression[])

Creates an IndexExpression representing the access to an indexed property.

public:
 static System::Linq::Expressions::IndexExpression ^ Property(System::Linq::Expressions::Expression ^ instance, System::String ^ propertyName, ... cli::array <System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression instance, string propertyName, params System.Linq.Expressions.Expression[] arguments);
static member Property : System.Linq.Expressions.Expression * string * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.IndexExpression
Public Shared Function Property (instance As Expression, propertyName As String, ParamArray arguments As Expression()) As IndexExpression

Parameters

instance
Expression

The object to which the property belongs. If the property is static/shared, it must be null.

propertyName
String

The name of the indexer.

arguments
Expression[]

An array of Expression objects that are used to index the property.

Returns

Property(Expression, PropertyInfo, Expression[])

Creates an IndexExpression representing the access to an indexed property.

public:
 static System::Linq::Expressions::IndexExpression ^ Property(System::Linq::Expressions::Expression ^ instance, System::Reflection::PropertyInfo ^ indexer, ... cli::array <System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression instance, System.Reflection.PropertyInfo indexer, params System.Linq.Expressions.Expression[] arguments);
static member Property : System.Linq.Expressions.Expression * System.Reflection.PropertyInfo * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.IndexExpression
Public Shared Function Property (instance As Expression, indexer As PropertyInfo, ParamArray arguments As Expression()) As IndexExpression

Parameters

instance
Expression

The object to which the property belongs. If the property is static/shared, it must be null.

indexer
PropertyInfo

The PropertyInfo that represents the property to index.

arguments
Expression[]

An array of Expression objects that are used to index the property.

Returns

Property(Expression, PropertyInfo, IEnumerable<Expression>)

Creates an IndexExpression representing the access to an indexed property.

public:
 static System::Linq::Expressions::IndexExpression ^ Property(System::Linq::Expressions::Expression ^ instance, System::Reflection::PropertyInfo ^ indexer, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ arguments);
public static System.Linq.Expressions.IndexExpression Property (System.Linq.Expressions.Expression instance, System.Reflection.PropertyInfo indexer, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments);
static member Property : System.Linq.Expressions.Expression * System.Reflection.PropertyInfo * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.IndexExpression
Public Shared Function Property (instance As Expression, indexer As PropertyInfo, arguments As IEnumerable(Of Expression)) As IndexExpression

Parameters

instance
Expression

The object to which the property belongs. If the property is static/shared, it must be null.

indexer
PropertyInfo

The PropertyInfo that represents the property to index.

arguments
IEnumerable<Expression>

An IEnumerable<T> of Expression objects that are used to index the property.

Returns

Property(Expression, Type, String)

Creates a MemberExpression accessing a property.

public:
 static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, Type ^ type, System::String ^ propertyName);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, Type type, string propertyName);
static member Property : System.Linq.Expressions.Expression * Type * string -> System.Linq.Expressions.MemberExpression

Parameters

expression
Expression

The containing object of the property. This can be null for static properties.

type
Type

The Type that contains the property.

propertyName
String

The property to be accessed.

Returns

Property(Expression, PropertyInfo)

Creates a MemberExpression that represents accessing a property.

public:
 static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, System::Reflection::PropertyInfo ^ property);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, System.Reflection.PropertyInfo property);
static member Property : System.Linq.Expressions.Expression * System.Reflection.PropertyInfo -> System.Linq.Expressions.MemberExpression

Parameters

expression
Expression

An Expression to set the Expression property equal to. This can be null for static properties.

property
PropertyInfo

The PropertyInfo to set the Member property equal to.

Returns

A MemberExpression that has the NodeType property equal to MemberAccess and the Expression and Member properties set to the specified values.

Exceptions

property is null.

-or-

The property that property represents is not static (Shared in Visual Basic) and expression is null.

expression.Type is not assignable to the declaring type of the property that property represents.

Remarks

The Type property of the resulting MemberExpression is equal to the PropertyType property of Member.

If the property represented by property is static (Shared in Visual Basic), expression can be null.

Property(Expression, MethodInfo)

Creates a MemberExpression that represents accessing a property by using a property accessor method.

public:
 static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, System::Reflection::MethodInfo ^ propertyAccessor);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, System.Reflection.MethodInfo propertyAccessor);
static member Property : System.Linq.Expressions.Expression * System.Reflection.MethodInfo -> System.Linq.Expressions.MemberExpression

Parameters

expression
Expression

An Expression to set the Expression property equal to. This can be null for static properties.

propertyAccessor
MethodInfo

The MethodInfo that represents a property accessor method.

Returns

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 accessed in propertyAccessor.

Exceptions

propertyAccessor is null.

-or-

The method that propertyAccessor represents is not static (Shared in Visual Basic) and expression is null.

expression.Type is not assignable to the declaring type of the method represented by propertyAccessor.

-or-

The method that propertyAccessor represents is not a property accessor method.

Remarks

The Type property of the resulting MemberExpression is equal to the PropertyType property of Member.

If the method represented by propertyAccessor is static (Shared in Visual Basic), expression can be null.

Property(Expression, String)

Creates a MemberExpression that represents accessing a property.

public:
 static System::Linq::Expressions::MemberExpression ^ Property(System::Linq::Expressions::Expression ^ expression, System::String ^ propertyName);
public static System.Linq.Expressions.MemberExpression Property (System.Linq.Expressions.Expression expression, string propertyName);
static member Property : System.Linq.Expressions.Expression * string -> System.Linq.Expressions.MemberExpression

Parameters

expression
Expression

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

propertyName
String

The name of a property to be accessed.

Returns

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

expression or propertyName is null.

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

Examples

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

' 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.
    Console.WriteLine(Expression.Lambda(Of Func(Of Integer))(propertyExpr).Compile()())
End Sub

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

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.

Applies to