递归获取物业放;子属性的一类 [英] Recursively Get Properties & Child Properties Of A Class
问题描述
我在做类似<一个href=\"http://stackoverflow.com/questions/4220991/recursively-get-properties-child-properties-of-an-object\">Recursively找物业放;儿童属性的对象,但我想用递归反射,以获取每个属性。而我从递归打印属性。
I was doing something like Recursively Get Properties & Child Properties Of An Object, but I wanted to use reflection recursively to get each properties. And I got the code from Recursively Print the properties.
与code的问题是:那就只向下一个级别,我不知道你怎么能自动使用反射获取所有属性?我刚由下列样品容器code:
The problem with the code is: it only goes one level down, I wonder how can you automatically get all the properties using reflection? I just made up the following sample Container code:
public class Container
{
public Bottle MyBottle { get; set; }
public List<Address> Addresses { get; set; }
public Container()
{
Address a = new Address();
a.AddressLine1 = "1 Main St";
a.AddressLine2 = "2 Main St";
Addresses = new List<Address>();
Addresses.Add(a);
MyBottle = new Bottle();
MyBottle.BottleName = "Big bottle";
MyBottle.BottageAge = 2;
}
}
public class Bottle
{
public string BottleName { get; set; }
public int BottageAge { get; set; }
}
public class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public List<SpecialFolder> SpecialFolders { get; set; }
public Address()
{
SpecialFolders = new List<SpecialFolder>();
SpecialFolder sf = new SpecialFolder();
sf.TemplateFolder = Environment.SpecialFolder.Templates.ToString();
sf.UserFolder = Environment.SpecialFolder.UserProfile.ToString();
SpecialFolders.Add(sf);
}
}
public class SpecialFolder
{
public string TemplateFolder { get; set; }
public string UserFolder { get; set; }
}
在Main方法:
static void Main(string[] args)
{
Container c = new Container();
PrintProperties(c);
}
public static void PrintProperties(object obj)
{
PrintProperties(obj, 0);
}
public static void PrintProperties(object obj, int indent)
{
if (obj == null) return;
string indentString = new string(' ', indent);
Type objType = obj.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(obj, null);
if (property.PropertyType.Assembly == objType.Assembly)
{
Console.WriteLine("{0}{1}:", indentString, property.Name);
PrintProperties(propValue, indent + 2);
}
else
{
Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
}
}
}
我希望能得到:
MyBottle:
BottleName: Big bottle
BottageAge: 2
Addresses:
AddressLine1: 1 Main St
AddressLine2: 2 Main St
SpecialFolders:
TemplateFolder: Templates
UserFolder: UserProfile
结果我现在得到:
The result I get now:
MyBottle:
BottleName: Big bottle
BottageAge: 2
Addresses: System.Collections.Generic.List`1[TreeViewReflectionExample.Address]
有人可以帮助我的PrintProperties方法?非常感谢你。
Can someone help me with the PrintProperties method? Thank you very much.
推荐答案
您有两个问题与code:
You have two problems with your code:
- ,因为条件
如果(property.PropertyType.Assembly == objType.Assembly)
你会忽略System.Collections中
如列表与LT;&GT;
- 您不要区别对待
propValue
是集合。因此,它会打印列表
属性,而不是它的元素属性。
- because of condition
if (property.PropertyType.Assembly == objType.Assembly)
you will omitSystem.Collections
likeList<>
- you do not treat differently
propValue
that are collections. Hence it will printList
properties, not its elements properties.
您可以更改到例如:
public void PrintProperties(object obj, int indent)
{
if (obj == null) return;
string indentString = new string(' ', indent);
Type objType = obj.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(obj, null);
var elems = propValue as IList;
if (elems != null)
{
foreach (var item in elems)
{
PrintProperties(item, indent + 3);
}
}
else
{
// This will not cut-off System.Collections because of the first check
if (property.PropertyType.Assembly == objType.Assembly)
{
Console.WriteLine("{0}{1}:", indentString, property.Name);
PrintProperties(propValue, indent + 2);
}
else
{
Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
}
}
}
}
这篇关于递归获取物业放;子属性的一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!