【发布时间】:2018-04-13 09:45:09
【问题描述】:
我有一个带有数据注释的类,需要从 Display 和属性 Name 中获取字符串列表。
我已经尝试了一些方法。 在方法 GetAttributesNames() 中。
internal class TVSystemViewData : BaseViewData
{
[Display(Name = "BoxType", Description = "")]
public String BoxType { get; set; }
[Display(Name = "BoxVendor", Description = "")]
public String BoxVendor { get; set; }
[Display(Name = "ClientId", Description = "")]
public String ClientId { get; set; }
[Display(Name = "HostName", Description = "")]
public String HostName { get; set; }
[Display(Name = "OSVersion", Description = "")]
public String OSVersion { get; set; }
[Display(Name = "SerialNumber", Description = "")]
public String SerialNumber { get; set; }
internal void GetAttributesNames()
{
var listOfFieldNames = typeof(TVSystemViewData)
.GetProperties()
.Select(x => x.GetCustomAttributes(typeof(DisplayAttribute), true))
.Where(x => x != null)
.ToList();
}
}
【问题讨论】:
-
那么这有什么作用或没有作用?您必须将属性转换为
DisplayAttribute才能访问其Name属性。
标签: c# .net data-annotations