Skip to main content
 首页 » 编程设计

c#之LINQ 获取类的属性

2024年02月20日13lexus

如何使用 linq 获取对象/类的属性列表?

public class Person  {  
public string Name   { get; set; }  
public string Age    { get; set; }  
public string Gender { get; set; }  
} 

我想{"姓名","年龄","性别"}。

请您参考如下方法:

正如之前的评论者所提到的,反射是您应该用来获取此信息的工具。

下面是一个小示例程序,它将从一个假设的“Person”类中检索和显示属性名称:

System.Type type = typeof(Person); 
System.Reflection.PropertyInfo[] properties = type.GetProperties(); 
 
foreach (System.Reflection.PropertyInfo property in properties) 
    Console.WriteLine(property.Name);