2012-08-06 83 views
0

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」內部對象的值不能。任何人有想法?謝謝!!

+0

中獲得類型作爲值,您應該將方法名稱的第一個字母重命名爲小寫字母。這件事可以解決你的問題。 – reporter 2012-08-06 11:29:24

+0

以上不是Java。我想這是C#。如果是這樣,它應該重新標記爲這樣。 – 2012-08-06 11:33:24

+0

AFAIK這完全是用'C#'編寫的我看不到你想如何使用'Java' – 2012-08-06 11:33:26

回答

1

您可以這樣更新方法:

private static void GetDTOProperties(IDictionary<string, object> dicProperties, object objectToSerialize) 
{ 
    Type typeOfObject = objectToSerialize is Type ? objectToSerialize as Type : objectToSerialize.GetType(); 
    PropertyInfo[] properties = typeOfObject.GetProperties(); 
    foreach (PropertyInfo property in properties) 
    { 
     object val = objectToSerialize is Type ? property.PropertyType : property.GetValue(objectToSerialize, null); 
     if (!property.PropertyType.IsClass || property.PropertyType.Equals(typeof(string))) 
     { 
     dicProperties.Add(string.Format("{0}_{1}", property.DeclaringType.Name.ToLower(), property.Name.ToLower()), val);      
     } 
     else 
     GetDTOProperties(dicProperties, val); 
    } 
} 

所以不會有帶BA對象的任何問題,可以發送實際的對象,該方法。如果您發送的是對象類型,那麼如果使用Java作爲程序員語言,則會在字典

+0

謝謝,Jleru,我不想要對象的類型,但它們的值。 – Kiwanax 2012-08-06 12:20:55

+0

比用GetDTOProperties(dicPropriedades,objEntidadeAluno)等對象調用該方法; – JleruOHeP 2012-08-06 12:29:56

相關問題