Heys,guys! 我想從我的反射物體得到的值,買我有一個問題:我可以從基本對象的值,但我不能從內心對象獲取值,就像這樣:通過反射獲得一個類的內部對象的值
public class Employee
{
public long EmployeeID { get; set; }
public string EmployeeNumber { get; set; }
public DateTime EmployeeAdmissionDate { get; set; }
public Person EmployeePerson { get; set; }
}
public class Person
{
public long PersonID { get; set; }
public string PersonName { get; set; }
public DateTime PersonBirthday { get; set; }
}
private static void GetDTOProperties(IDictionary<string, object> dicProperties, Type objectToSerialize)
{
Type typeOfObject = objectToSerialize is Type ? objectToSerialize as Type : objectToSerialize.GetType();
PropertyInfo[] properties = typeOfObject.GetProperties();
foreach (PropertyInfo property in properties)
{
if (!property.PropertyType.IsClass || property.PropertyType.Equals(typeof(string)))
dicProperties.Add(string.Format("{0}_{1}", property.DeclaringType.Name.ToLower(), property.Name.ToLower()), property.GetValue(typeOfObject, null));
else
GetDTOProperties(dicProperties, property.PropertyType);
}
}
public static void Main(string[] args)
{
Employee objEmployee = new Employee();
objEmployee.EmployeeID = 1;
objEmployee.EmployeeNumber = 457435;
objEmployee.EmployeeAdmissionDate = DateTime.Now;
objEmployee.EmployeePerson = new EmployeePerson();
objEmployee.EmployeePerson.PersonID = 123;
objEmployee.EmployeePerson.PersonName = "Kiwanax";
objEmployee.EmployeePerson.PersonBirthday = DateTime.Now;
IDictionary<string, object> dicProperties= new Dictionary<string, object>();
GetDTOProperties(dicPropriedades, objEntidadeAluno.GetType());
foreach (string m in dicProperties.Keys)
Console.WriteLine(m + " - " + dicProperties[m]);
Console.ReadLine();
}
我可以得到的基礎值,但是「Person」內部對象的值不能。任何人有想法?謝謝!!
中獲得類型作爲值,您應該將方法名稱的第一個字母重命名爲小寫字母。這件事可以解決你的問題。 – reporter 2012-08-06 11:29:24
以上不是Java。我想這是C#。如果是這樣,它應該重新標記爲這樣。 – 2012-08-06 11:33:24
AFAIK這完全是用'C#'編寫的我看不到你想如何使用'Java' – 2012-08-06 11:33:26