获取对象中底层对象的所有属性

时间:2016-06-24 02:08:56

标签: c#-4.0 properties system.reflection

    class Employee
    {
        public string EmployeeId;
        public UserGroup[] UserGroups;
        public Department Department;
        public List<Address> Address;
    }
    class UserGroup
    {
        public int GroupId;
        public string GroupName;
        public string UserName;
        public string Password;
    }
    public class Department
    {
        public int DeptId;
        public string DeptName;
    }
    public class Address
    {
        public string Street;
        public string ZipCode;
        public decimal Latitude;
        public decimal Longitude;
    }
    /// <summary>
    /// This method should loop and read all the properties of Employee object and and assign value to that property
    /// Ex: if propertyName is EmployeeId, propertyValue must be assigned to it.
    /// Ex: if propertyName is GroupName, propertyValue must be assigned to it.
    /// Ex: if propertyName is DeptName, propertyValue must be assigned to it.
    /// Ex: if propertyName is Latitude, propertyValue must be assigned to it.
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="propertyName"></param>
    /// <param name="propertyValue"></param>
    public static AssignValueToProperty(Employee obj, string propertyName, string propertyValue)
    {
        //Implementation Pending
    }

给定Employee对象,PropertyName和PropertyValue,我应该能够遍历其所有内部对象的属性并为该属性赋值。有人可以帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

我们可以使用 C#.NET 中的反射以及该属性的getset值来访问对象属性。

我认为你正在寻找类似的东西。

考虑您的Employee对象是

    class Employee
    {
        public string EmployeeId;
        public UserGroup[] UserGroups;
        public Department Department;
        public List<Address> Address;
    }

您想循环遍历对象的所有属性。你可以这样做:

Employee objEmp = new Employee();

//loop

foreach(var property in objEmp.GetType().GetProperties())
{
   AssignValueToProperty(objEmp, property.Name, "{Your Value}");
}

然后在您的AssignValueToProperty方法中,您将拥有类似

的内容
public static AssignValueToProperty(Employee obj, string propertyName, string propertyValue)
{
    objEmp.GetType().GetProperty(propertyName).SetValue(objEmp, propertyValue, null);
}

此外,我想补充的是,您的对象结构具有UserGroup[] UserGroups等属性,其中UserGroup是另一个对象。你不认为你的AssignValueToProperty方法在这里不起作用,因为当你在Employee对象中有不同的数据类型时,它接受属性值作为字符串数据类型。因此,您所要做的就是循环遍历多个对象属性,如上面给出的方式,最后将它们设置为员工对象。

您也可以通过检查datatypes来设置对象。的

foreach(var property in objEmp.GetType().GetProperties())
{
   // Get PropertyType
   string datatype = property.PropertyType.Name;
   // Apply conditions on the datatype basis
   if (datatype.Equals("string") && property.Name.Equals("EmployeeId"))
   {
      // assign string values
      objEmp.GetType().GetProperty(property.Name).SetValue(objEmp, "{your string value}", null);
   }
   else if (datatype.Equals("UserGroup") && property.Name.Equals("UserGroups"))
   {
      // assign object value
      objEmp.GetType().GetProperty(property.Name).SetValue(objEmp, "{your UserGroup Object}", null);
   }
   // so on other properties
}
// And Finally Use `objEmployee` with all properties values set.

<强>奖金

您还可以使用lambda表达式查找和设置属性,如下所示:

objEmp.GetType().GetProperties().Where(x => x.Name.Equals("{PropertyName}") && x.PropertyType.Name.Equals("{Target datatype}")).FirstOrDefault().SetValue({Your Object}, {Your Value}, null);

如果您需要在运行时创建类对象,则方法定义如下:

public static AssignValueToProperty(string obj, string propertyName, string propertyValue)
{
    Type type = Type.GetType(obj, true);
    object objEmp = Activator.CreateInstance(type);
    objEmp.GetType().GetProperty(propertyName).SetValue(objEmp, propertyValue, null);
}

希望有助于......:)