C# 在模型实体中实现标题属性?

C# 在模型实体中实现标题属性?,c#,metadata,caption,C#,Metadata,Caption,最近,我们正在将Datatable转换为List模型。。 我有一个名为Person(personid,PersonChildId)的类,我将我的类实现为 public class Person { public Int64 PerosnId{ get; set; } public Int64 PersonChildID{ get; set; } } 我使用simple for loop成功地将我的数据表转换为列表。但现在的问题是,我们知道数据列有一个名为标题的属性。但不幸的是

最近,我们正在将
Datatable
转换为
List
模型。。 我有一个名为
Person
personid
PersonChildId
)的类,我将我的类实现为

public class Person 
{
    public Int64 PerosnId{ get; set; }
    public Int64 PersonChildID{ get; set; }
}
我使用simple for loop成功地将我的
数据表
转换为
列表
。但现在的问题是,我们知道
数据列
有一个名为
标题
的属性。但不幸的是,模型没有。
因此,我们需要如何为此类场景的模型实现标题属性。

使用如下属性:

您可以使用以下方法获取属性的值:

var caption = property.GetCustomAttributes(typeof(DisplayNameAttribute), true)
      .Cast<DisplayNameAttribute>.Single().DisplayName;
var caption=property.GetCustomAttributes(typeof(DisplayNameAttribute),true)
.Cast.Single().DisplayName;
使用如下属性:

您可以使用以下方法获取属性的值:

var caption = property.GetCustomAttributes(typeof(DisplayNameAttribute), true)
      .Cast<DisplayNameAttribute>.Single().DisplayName;
var caption=property.GetCustomAttributes(typeof(DisplayNameAttribute),true)
.Cast.Single().DisplayName;


通过添加
Caption
属性?!整个Person类的Caption属性?如果我们将PersonId和PersonChildId视为两个不同的列,那么这两个列都需要不同的标题…那么我们如何处理这个问题哦…为什么要投反对票....这个问题有什么问题吗yaar?哦,现在我明白你的意思了,请看下面我的答案。你还有p吗问题?通过添加
Caption
property?!整个Person类的Caption属性?如果我们将PersonId和PersonChildId视为两个不同的列,那么这两个列都需要不同的标题…那么我们如何处理这个问题哦…为什么要投反对票…。这个问题出了什么问题啊?哦,现在我明白你的意思了,请看下面我的答案。你还有吗问题?我们可以在运行时设置属性吗?@Bhuvan,是的,这是可能的。hi-gdoron,好的,但是我们如何访问显示名称。例如,如果我们想访问PersonID,我们可以将它用作objPerson.PersonID…同样,我们如何访问中PersonID的显示名称code@Bhuvan,添加到答案中。@gdoron..Hi..i没有得到值当我试图在客户端以“Person objPerson=new Person();var caption=objPerson.GetType().GetProperty(“PersonId”).GetCustomAttributes(typeof(DisplayNameAttribute),true).Cast().Single().DisplayName;”的形式检索属性时…你能告诉我从客户端访问Displayname的方法是否正确吗?我们能在运行时设置属性吗?@Bhuvan,是的,这是可能的。嗨,gdoron,好的,但是我们如何访问display name。例如,如果我们想访问PersonID,我们可以将其用作objPerson.PersonID…以同样的方式访问display name of中的人物code@Bhuvan,添加到答案中。@gdoron..Hi..当我试图在客户端以“Person objPerson=new Person();var caption=objPerson.GetType().GetProperty(“PersonId”).GetCustomAttributes(typeof(DisplayNameAttribute),true.Cast().Single()的形式检索属性时,我没有得到属性的值.DisplayName;”请你告诉我,我是否正确地访问了CelpTr.Mr穆罕默德的DISPLAYND,请考虑添加更多的信息,说明你的代码可能如何解决这个问题。欢迎使用堆栈溢出。虽然这个代码片段可以解决这个问题,但是确实有助于提高你的文章质量。记住你正在回答这个问题。在未来的读者中,那些人可能不知道你的代码建议的原因。也请尽量不要用解释性的注释来填充你的代码,这降低了代码和解释的可读性。穆罕默德先生,考虑添加更多的信息,你的代码可能如何解决这个问题。欢迎使用堆栈溢出。代码片段可能会解决问题,确实有助于提高您的文章质量。请记住,您将在将来为读者回答问题,而这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满您的代码,这会降低代码和文章的可读性解释!
    public int ID { get; set; }

    [System.ComponentModel.DisplayName("Full_Name:")]
    public string FullName { get; set; }

    public string Adress { get; set; }

    public string SSID { get; set; }