Click or drag to resize
DigitalRuneObjectHelperGetPropertyNameTObject, TProperty Method (TObject, ExpressionFuncTObject, TProperty)
Retrieves the name of a given object's property identified by a lambda expression.

Namespace: DigitalRune
Assembly: DigitalRune (in DigitalRune.dll) Version: 1.20.0.0 (1.20.1.14552)
Syntax
public static string GetPropertyName<TObject, TProperty>(
	this TObject this,
	Expression<Func<TObject, TProperty>> expression
)

Parameters

this
Type: TObject
The object containing the property.
expression
Type: System.Linq.ExpressionsExpressionFuncTObject, TProperty
A lambda expression selecting the property from the containing object.

Type Parameters

TObject
The type of object containing the property.
TProperty
The type of the property.

Return Value

Type: String
The name of the property accessed by expression.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type . When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullExceptionexpression is .
ArgumentExceptionexpression does not represent an expression accessing a property.
Remarks
It is recommended to use this method to access the name of a property instead of using hard-coded string values.
Examples
The following example shows how to retrieve the name of a property as a string.
C#
class MyClass
{
  public int MyProperty { get; set; }
}

...

MyClass myClass = new MyClass();

// The following line retrieves the property name as a string.
string propertyName = myClass.GetPropertyName(o => o.MyProperty);
See Also