2013-05-13 62 views
2

的我來到了這個網站(http://snipplr.com/view.php?codeview&id=17637),這說明像這樣使用反射:C#和使用反射

public class Person 
    { 
     public int Age { get; set; } 
     public string Name { get; set; } 
    } 

    private void button2_Click_1(object sender, EventArgs e) 
    { 
     var person = new Person { Age = 30, Name = "Tony Montana" }; 
     var properties = typeof(Person).GetProperties(); 
     foreach (PropertyInfo property in properties) 
     { 
      Console.WriteLine("{0} = {1}", property.Name, property.GetValue(person, null)); 
     } 
    } 

的codesnippet上述會給你: 年齡:30 名稱:託尼·蒙大拿

如果我們增加了「小子」的類「AnotherPerson」像這樣

public class Kid 
    { 
     public int KidAge { get; set; } 
     public string KidName { get; set; } 
    } 
    public class AnotherPerson 
    { 
     public int Age { get; set; } 
     public string Name { get; set; } 
     public Kid Kid { get; set; } 
    } 

這個片段;

private void button3_Click(object sender, EventArgs e) 
    { 
     var anotherPerson = new AnotherPerson { Age = 30, Name = "Tony Montana", Kid = new Kid { KidAge = 10, KidName = "SomeName" } }; 
     var properties = typeof(AnotherPerson).GetProperties(); 
     foreach (PropertyInfo property in properties) 
     { 
      Console.WriteLine("{0} = {1}", property.Name, property.GetValue(anotherPerson, null)); 
     } 
    } 

給我: 年齡:30 名稱:託尼·蒙大拿 小子:ProjectName.Form1 +兒童

不太什麼,我一直在尋找....我可以使用反射來遍歷低谷「孩子「呢?建議?

+2

對'kid'對象做同樣的事情 – I4V 2013-05-13 13:43:31

+1

您是否真的對某種形式的序列化感興趣,比如JSON? http://json.codeplex.com/ – 2013-05-13 13:43:43

+0

你將需要遞歸調用你的輸出方法,並期待看看一個屬性是否是一個對象,我相信這個問題做到了這一切:http://stackoverflow.com/questions/6196413/ how-to-recursively-print-the-values-of-an-objects-properties-using-reflection – gordatron 2013-05-13 13:44:55

回答

0

可能有一個這樣的幫手方法?

public List<KeyValuePair<string, object>> GetPropertiesRecursive(object instance) 
{ 
    List<KeyValuePair<string, object>> propValues = new List<KeyValuePair<string, object>>(); 
    foreach(PropertyInfo prop in instance.GetType().GetProperties()) 
    { 
    propValues.Add(new KeyValuePair<string, object>(prop.Name, prop.GetValue(instance, null)); 
    propValues.AddRange(GetPropertiesRecursive(prop.GetValue(instance)); 
    } 
} 

呼叫作爲 - GetPropertiesRecursive(anotherPerson)

+0

你不需要'Type'參數 – I4V 2013-05-13 13:48:45

+0

@ I4V正要改變這個;) – LukeHennerley 2013-05-13 13:49:58

0
var anotherPerson = new AnotherPerson { Age = 30, Name = "Tony Montana", Kid = new Kid { KidAge = 10, KidName = "SomeName" } }; 
    var properties = typeof(AnotherPerson).GetProperties(); 
    foreach (PropertyInfo property in properties) 
    { 

     if (property.PropertyType == typeof(Kid)){ 
      var pp = typeof(Kid).GetProperties(); 
      Console.WriteLine("Kid:"); 
      foreach (PropertyInfo p in pp) 
      { 
       Console.WriteLine("\t{0} = {1}", p.Name, p.GetValue(anotherPerson.Kid, null)); 
      } 
     } 
     else 
      Console.WriteLine("{0} = {1}", property.Name, property.GetValue(anotherPerson, null)); 
    } 
+0

如果OP向'AnotherPerson'添加了一個'青春期類型'?在這種情況下反思的想法可能會使其對未來儘可能通用:) – LukeHennerley 2013-05-13 13:52:30

+1

這太過僵化了。 – James 2013-05-13 13:52:35

+0

確實,因爲它是_flexible_,你應該對待原始類型,數組,列表,字典和索引屬性。 – 2013-05-13 14:03:44

0

的代碼循環通過性能和用途ToString(通過Console.WriteLine使用String.Format方法),以獲得屬性值的字符串表示。

如果你想改變如何Kid屬性值顯示,你基本上有兩種選擇:

  1. 入住反射代碼屬性(property.PropertyType)的類型,這樣就可以做一些事情Kid類型不同。

  2. 覆蓋Kid類中的ToString方法,以便它返回任何要顯示爲值的字符串。

0

一種方法,做一些可以遞歸屬性中獲得屬性的值:如果你腦子裏想的實際目的

public static string GetProperties(object obj) 
{ 
    var t = obj.GetType(); 
    var properties = t.GetProperties(); 
    var s = ""; 
    foreach (PropertyInfo property in properties) 
    { 
     object propValue = property.GetValue(obj, new object[0]); 
     string propertyToString; 
     if (t.GetMethod("ToString").DeclaringType != typeof(object)) 
      propertyToString = propValue.ToString(); 
     else if (typeof(IConvertible).IsAssignableFrom(property.PropertyType)) 
      propertyToString = Convert.ToString(propValue); 
     else 
      propertyToString = GetProperties(propValue); 

     s += string.Format("'{0}' = '{1}'", property.Name, propertyToString); 
    } 
    return s; 
} 

,JSON(或其他)序列化可能會更合適,如使用JSON.net會產生一個類似的字符串。