如何仅在类中获取所有属性名称

时间:2014-04-22 07:10:02

标签: c# class reflection

如何获得以下类的所有属性:

public class Employee
{
    int employeeID;
    string lastName;    //  should be (20)  chars only
    string firstName;   //  should be (10)  chars only
    string title;       //  should be (30)  chars only
    string address;     //  should be (60)  chars only
    string city;        //  should be (15)  chars only
    string region;      //  should be (15)  chars only
    string postalCode;  //  should be (10)  chars only
    string country;     //  should be (15)  chars only
    string extension;   //  should be (4)   chars only
    string homePhone;



    public int EmployeeID
    {
        get
        {
            return employeeID;
        }
        set
        {
            employeeID = value;
        }
    }

    public string LastName
    {
        get
        {
            return lastName;
        }
        set
        {
            lastName  = value;
        }
    }

    public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            firstName = value;
        }
    }

    public string Title
    {
        get
        {
            return title;
        }
        set
        {
            title = value;
        }
    }

    public string Address
    {
        get
        {
            return address;
        }
        set
        {
            address = value;
        }
    }

    public string City
    {
        get
        {
            return city;
        }
        set
        {
            city = value;
        }
    }

    public string Region
    {
        get
        {
            return region;
        }
        set
        {
            region = value;
        }
    }

    public string PostalCode
    {
        get
        {
            return postalCode;
        }
        set
        {
            postalCode = value;
        }
    }

    public string Country
    {
        get
        {
            return country;
        }
        set
        {
            country = value;
        }
    }

    public string Extension
    {
        get
        {
            return extension;
        }
        set
        {
            extension = value;
        }
    }
    public string HomePhone
    {
        get { return homePhone; }
        set { homePhone = value; }
    }

}

我尝试使用reflection / PropertyInfo但有轻微问题,数据类型包含在列表中:

  public PropertyInfo[] getPropertyInfo()
  {
        PropertyInfo[] propertyInfos;
        propertyInfos = typeof(Employee).GetProperties();
        return propertyInfos;
   }

i need to get the Property name not the datatype

提前谢谢。

1 个答案:

答案 0 :(得分:3)

您的方法为每个属性返回PropertyInfo数据结构,其中包含属性名称,属性返回类型等信息。如果您只想要名称,请访问Name property。< / p>

要将PropertyInfo的数组映射到属性名称的集合,可以使用LINQ Select扩展方法:

var propertyNames = getPropertyInfo().Select(p => p.Name).ToList();

PS:您可以考虑使用auto-implemented properties来减少班级中的代码数量:

...
public string LastName { get; set; }    //  should be (20)  chars only
public string FirstName { get; set; }   //  should be (10)  chars only
...