Read field aliases

A user class is defined for display:

public class UserModel
    {
        [Display(Name = "姓名")]
        public string UserName { get; set; }

        [Display(Name = "手机号")]
        public string UserPhone { get; set; }
    }

Reads the display name of the user class:

void GetTitle<T>(List<T> sourse)
        {
            var t = typeof(T).GetProperties();
            foreach (var item in t)
            {
                var displayAttr = item.GetCustomAttributes(typeof(DisplayAttribute), false);
                if (displayAttr != null && displayAttr.Length != 0)
                {
                    var currentAttr = displayAttr[0] as DisplayAttribute;
                    string displayName = currentAttr.Name;
                }
            }
        }

 

Guess you like

Origin www.cnblogs.com/Gylianger/p/11226079.html