2013-05-13 63 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会产生一个类似的字符串。