| ObjectHelperGetPropertyNameTObject, TProperty Method (TObject, ExpressionFuncTObject, TProperty) |
Retrieves the name of a given object's property identified by a lambda expression.
Namespace: DigitalRuneAssembly: 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
)
<ExtensionAttribute>
Public Shared Function GetPropertyName(Of TObject, TProperty) (
this As TObject,
expression As Expression(Of Func(Of TObject, TProperty))
) As String
public:
[ExtensionAttribute]
generic<typename TObject, typename TProperty>
static String^ GetPropertyName(
TObject this,
Expression<Func<TObject, TProperty>^>^ expression
)
[<ExtensionAttribute>]
static member GetPropertyName :
this : 'TObject *
expression : Expression<Func<'TObject, 'TProperty>> -> string
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:
StringThe 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 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.
class MyClass
{
public int MyProperty { get; set; }
}
...
MyClass myClass = new MyClass();
string propertyName = myClass.GetPropertyName(o => o.MyProperty);
See Also