Lambda expressions

From no name for this wiki
Jump to: navigation, search
string GetPropertyName<T>(Expression<Func<T>> property)
{
            var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
            if (propertyInfo == null)
            {
                throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
            }
            return propertyInfo.Name;
}
 
GetPropertyName(() => this.Name);