2

我来到了这个网站(http://snipplr.com/view.php?codeview&id=17637),它说明了反射的使用,如下所示:

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));
        }
    }

上面的代码片段将为您提供: 年龄:30 姓名:Tony Montana

如果我们像这样将“Kid”添加到“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 姓名:Tony Montana 孩子:ProjectName.Form1+Kid

不完全是我想要的......我可以使用反射来迭代槽“Kid”吗?建议?

4

4 回答 4

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)

于 2013-05-13T13:47:29.143 回答
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会产生类似的字符串。

于 2013-05-13T14:00:48.433 回答
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));
    }
于 2013-05-13T13:50:39.360 回答
0

代码循环遍历属性并使用ToString(通过使用的String.Format方法Console.WriteLine)获取属性值的字符串表示形式。

如果要更改Kid属性值的显示方式,基本上有两种选择:

  1. 检查反射代码中的属性(property.PropertyType)的类型,以便您可以对Kid类型进行不同的操作。

  2. 覆盖类ToString中的方法Kid,以便它返回您想要显示为值的任何字符串。

于 2013-05-13T13:51:24.490 回答