C# 如何获取DataAnnotation显示名称列表
我有一个带有数据注释的类,需要从Display和attributename中获取字符串列表 我已经尝试了一些方法。 在方法GetAttributesNames()中 这可能会有帮助C# 如何获取DataAnnotation显示名称列表,c#,.net,data-annotations,C#,.net,Data Annotations,我有一个带有数据注释的类,需要从Display和attributename中获取字符串列表 我已经尝试了一些方法。 在方法GetAttributesNames()中 这可能会有帮助 internal List<string> GetAttributesNames() //changed type returned { return typeof(TVSystemViewData) .GetProperties()
internal List<string> GetAttributesNames() //changed type returned
{
return typeof(TVSystemViewData)
.GetProperties() //so far like you did
.SelectMany(x=>x.GetCustomAttributes(typeof(DisplayAttribute),true) //select many because can have multiple attributes
.Select(e=>((DisplayAttribute)e))) //change type from generic attribute to DisplayAttribute
.Where(x => x != null).Select( x => x.Name) //select not null and take only name
.ToList(); // you know ;)
}
internal List GetAttributesNames()//返回已更改的类型
{
返回类型(TVSystemViewData)
.GetProperties()//到目前为止与您一样
.SelectMany(x=>x.GetCustomAttributes(typeof(DisplayAttribute),true)//选择many,因为可以有多个属性
.Select(e=>((DisplayAttribute)e))//将类型从泛型属性更改为DisplayAttribute
.Where(x=>x!=null)。选择(x=>x.Name)//选择NOTNULL并只取名称
.ToList();//你知道;)
}
希望这有帮助这可能会有帮助
internal List<string> GetAttributesNames() //changed type returned
{
return typeof(TVSystemViewData)
.GetProperties() //so far like you did
.SelectMany(x=>x.GetCustomAttributes(typeof(DisplayAttribute),true) //select many because can have multiple attributes
.Select(e=>((DisplayAttribute)e))) //change type from generic attribute to DisplayAttribute
.Where(x => x != null).Select( x => x.Name) //select not null and take only name
.ToList(); // you know ;)
}
internal List GetAttributesNames()//返回已更改的类型
{
返回类型(TVSystemViewData)
.GetProperties()//到目前为止与您一样
.SelectMany(x=>x.GetCustomAttributes(typeof(DisplayAttribute),true)//选择many,因为可以有多个属性
.Select(e=>((DisplayAttribute)e))//将类型从泛型属性更改为DisplayAttribute
.Where(x=>x!=null)。选择(x=>x.Name)//选择NOTNULL并只取名称
.ToList();//你知道;)
}
希望这有帮助试试这个:
class Program
{
static void Main(string[] args)
{
var data = new TVSystemViewData();
var listOfDisplayNames = data.GetAttributesNames();
}
}
internal class TVSystemViewData
{
[Display(Name = "XXXXX", 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 List<string> GetAttributesNames()
{
return typeof(TVSystemViewData)
.GetProperties()
.Select(x => ((DisplayAttribute) x.GetCustomAttribute(typeof(DisplayAttribute), true)).Name)
.ToList();
}
}
类程序
{
静态void Main(字符串[]参数)
{
var数据=新的TVSystemViewData();
var listOfDisplayNames=data.GetAttributesNames();
}
}
内部类TVSystemViewData
{
[显示(Name=“XXXXX”,Description=”“)]
公共字符串BoxType{get;set;}
[显示(Name=“BoxVendor”,Description=”“)]
公共字符串BoxVendor{get;set;}
[显示(Name=“ClientId”,Description=”“)]
公共字符串ClientId{get;set;}
[显示(Name=“HostName”,Description=”“)]
公共字符串主机名{get;set;}
[显示(Name=“OSVersion”,Description=”“)]
公共字符串OSVersion{get;set;}
[显示(Name=“SerialNumber”,Description=”“)]
公共字符串序列号{get;set;}
内部列表GetAttributesNames()
{
返回类型(TVSystemViewData)
.GetProperties()
.Select(x=>((DisplayAttribute)x.GetCustomAttribute(typeof(DisplayAttribute),true)).Name)
.ToList();
}
}
试试这个:
class Program
{
static void Main(string[] args)
{
var data = new TVSystemViewData();
var listOfDisplayNames = data.GetAttributesNames();
}
}
internal class TVSystemViewData
{
[Display(Name = "XXXXX", 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 List<string> GetAttributesNames()
{
return typeof(TVSystemViewData)
.GetProperties()
.Select(x => ((DisplayAttribute) x.GetCustomAttribute(typeof(DisplayAttribute), true)).Name)
.ToList();
}
}
类程序
{
静态void Main(字符串[]参数)
{
var数据=新的TVSystemViewData();
var listOfDisplayNames=data.GetAttributesNames();
}
}
内部类TVSystemViewData
{
[显示(Name=“XXXXX”,Description=”“)]
公共字符串BoxType{get;set;}
[显示(Name=“BoxVendor”,Description=”“)]
公共字符串BoxVendor{get;set;}
[显示(Name=“ClientId”,Description=”“)]
公共字符串ClientId{get;set;}
[显示(Name=“HostName”,Description=”“)]
公共字符串主机名{get;set;}
[显示(Name=“OSVersion”,Description=”“)]
公共字符串OSVersion{get;set;}
[显示(Name=“SerialNumber”,Description=”“)]
公共字符串序列号{get;set;}
内部列表GetAttributesNames()
{
返回类型(TVSystemViewData)
.GetProperties()
.Select(x=>((DisplayAttribute)x.GetCustomAttribute(typeof(DisplayAttribute),true)).Name)
.ToList();
}
}
那么这有什么用呢?您必须将属性强制转换为DisplayAttribute
,才能访问它们的Name
属性。那么这有什么用呢?您必须将属性强制转换为DisplayAttribute
,才能访问它们的Name
属性。