Expression.PropertyOrField Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Creates a MemberExpression that represents accessing a property or field.
Namespace: System.Linq.Expressions
Assembly: System.Core (in System.Core.dll)
Syntax
'Declaration
Public Shared Function PropertyOrField ( _
expression As Expression, _
propertyOrFieldName As String _
) As MemberExpression
public static MemberExpression PropertyOrField(
Expression expression,
string propertyOrFieldName
)
Parameters
- expression
Type: System.Linq.Expressions.Expression
An Expression whose Type contains a property or field named propertyOrFieldName. This can be null for static members.
- propertyOrFieldName
Type: System.String
The name of a property or field 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 or FieldInfo that represents the property or field denoted by propertyOrFieldName.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | expression or propertyOrFieldName is nulla null reference (Nothing in Visual Basic). |
ArgumentException | No property or field named propertyOrFieldName is defined in expression.Type or its base types. |
Remarks
The 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.
This method searches expression.Type and its base types for a property or field that has the name propertyOrFieldName. Public properties and fields are given preference over non-public properties and fields. Also, properties are given preference over fields. If 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.
Examples
The following example shows how to create an expression that represents accessing a property or field.
' 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.
outputBlock.Text &= Expression.Lambda(Of Func(Of Integer))(memberExpr).Compile()() & vbCrLf
End Sub
' This code example produces the following output:
'
' 40
// Add the following directive to your file:
// using System.Linq.Expressions;
class TestClass
{
public int sample { get; set; }
}
static void TestPropertyOrField(System.Windows.Controls.TextBlock outputBlock)
{
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.
outputBlock.Text += Expression.Lambda<Func<int>>(memberExpr).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.