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

c#
1919
2024/4/13 15:27:49
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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类,该类具有Name和Age属性。然后,在Main方法中,我们使用typeof(Person).GetProperties()方法获取Person类的所有属性,并遍历打印每个属性的名称。

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

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: c# sendkeys怎样实现