using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
public static void Main()
Console.WriteLine("Solution 1: Iterate all properties in class");
PropertyInfo[] props = typeof(Model).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
foreach (PropertyInfo prop in props)
Console.WriteLine(prop.ToName());
Console.WriteLine("---");
Console.WriteLine("Solution 2: Get property by expression");
Console.WriteLine(ReflectionExtensions.ToNameByExpression((Model m) => m.FromDate));
public static class ReflectionExtensions
public static string ToName(this PropertyInfo propertyInfo)
object[] attributes = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), false);
if (attributes != null && attributes.Any())
return ((DisplayAttribute)attributes[0]).Name;
return propertyInfo.Name;
return propertyInfo.Name;
public static string ToNameByExpression<T, P>(Expression<Func<T, P>> propertyExpression) where T : new ()
MemberExpression expression = propertyExpression.Body as MemberExpression;
return (expression.Member as PropertyInfo)
[Display(Name = "From Date")]
public DateTime FromDate { get; set; }