温馨提示×

c#怎么获取实体类的属性名

c#
小亿
271
2024-05-11 15:27:49
栏目: 编程语言

在C#中,你可以使用反射来获取实体类的属性名。以下是一个示例代码:

using System; using System.Reflection; class Program { static void Main() { var person = new Person(); PropertyInfo[] properties = typeof(Person).GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine(property.Name); } } } class Person { public string Name { get; set; } public int Age { get; set; } } 

在上面的示例中,我们首先创建了一个Person类,该类具有NameAge属性。然后,在Main方法中,我们使用typeof(Person).GetProperties()方法获取Person类的所有属性,并遍历打印每个属性的名称。

通过这种方式,你可以获取任意实体类的属性名。

0