Kum*_*mar 22 c# asp.net reflection
我有以下两个类:
public class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class Employee
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public Address EmployeeAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个employee类的实例如下:
var emp1Address = new Address();
emp1Address.AddressLine1 = "Microsoft Corporation";
emp1Address.AddressLine2 = "One Microsoft Way";
emp1Address.City = "Redmond";
emp1Address.State = "WA";
emp1Address.Zip = "98052-6399";
var emp1 = new Employee();
emp1.FirstName = "Bill";
emp1.LastName = "Gates";
emp1.EmployeeAddress = emp1Address;
Run Code Online (Sandbox Code Playgroud)
我有一个方法,它根据属性名称获取属性值,如下所示:
public object GetPropertyValue(object obj ,string propertyName)
{
var objType = obj.GetType();
var prop = objType.GetProperty(propertyName);
return prop.GetValue(obj, null);
}
Run Code Online (Sandbox Code Playgroud)
上面的方法适用于调用,GetPropertyValue(emp1, "FirstName")但如果我尝试GetPropertyValue(emp1, "Address.AddressLine1")它会引发异常,因为objType.GetProperty(propertyName);无法找到嵌套的对象属性值.有没有办法来解决这个问题?
小智 22
public object GetPropertyValue(object obj, string propertyName)
{
foreach (var prop in propertyName.Split('.').Select(s => obj.GetType().GetProperty(s)))
obj = prop.GetValue(obj, null);
return obj;
}
Run Code Online (Sandbox Code Playgroud)
谢谢,我来到这里寻找同样问题的答案.我最终修改了原始方法以支持嵌套属性.这应该比必须进行嵌套方法调用更加健壮,这可能最终导致超过2个嵌套级别的繁琐.
Ami*_*obo 13
var address = GetPropertyValue(GetPropertyValue(emp1, "Address"), "AddressLine1");
Run Code Online (Sandbox Code Playgroud)
对象Employee没有名为"Address.AddressLine1"的单个属性,它有一个名为"Address"的属性,该属性本身具有名为"AddressLine1"的属性.
我使用此方法从属性(无限数量的嵌套属性)获取值,如下所示:
"属性"
"街道地址"
"Address.Country.Name"
public static object GetPropertyValue(object src, string propName)
{
if (src == null) throw new ArgumentException("Value cannot be null.", "src");
if (propName == null) throw new ArgumentException("Value cannot be null.", "propName");
if(propName.Contains("."))//complex type nested
{
var temp = propName.Split(new char[] { '.' }, 2);
return GetPropertyValue(GetPropertyValue(src, temp[0]), temp[1]);
}
else
{
var prop = src.GetType().GetProperty(propName);
return prop != null ? prop.GetValue(src, null) : null;
}
}
Run Code Online (Sandbox Code Playgroud)
这里的小提琴:https://dotnetfiddle.net/PvKRH0
这适用于无限数量的嵌套属性.
public object GetPropertyValue(object obj, string propertyName)
{
var _propertyNames = propertyName.Split('.');
for (var i = 0; i < _propertyNames.Length; i++)
{
if (obj != null)
{
var _propertyInfo = obj.GetType().GetProperty(_propertyNames[i]);
if (_propertyInfo != null)
obj = _propertyInfo.GetValue(obj);
else
obj = null;
}
}
return obj;
}
Run Code Online (Sandbox Code Playgroud)
用法:
GetPropertyValue(_employee, "Firstname");
GetPropertyValue(_employee, "Address.State");
GetPropertyValue(_employee, "Address.Country.Name");
Run Code Online (Sandbox Code Playgroud)