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,我应该能够遍历其内部对象的所有属性并为该属性赋值。有人能帮我一下吗?
在 c#中可以使用反射来访问对象属性。. NET以及get
和set
值对该属性。
我想你正在找这样的东西。
考虑给定的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
对象中有不同的数据类型。因此,您所要做的就是像上面给出的那样循环遍历多个对象属性,并最终将它们设置为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);
}
Hope That help…:)