Wednesday, August 7, 2013

C# - How to get Property Name as string by Property Type

Create a static class for Property Helper with GetPropertyName method

public static class PropertyHelper<T>
    {
        public static string GetPropertyName<TPropObject>(Expression<Func<T, TPropObject>> propertyValue)
        {
            return ((MemberExpression)propertyValue.Body).Member.Name;
        }
    }


Call like below to get the PropertyName as string


PropertyHelper<YourClass>.GetPropertyName(r => r.YourProperty)

No comments:

Post a Comment