What I am trying to do is a sort of Recursion "search" or listing of all the
properties of a type.
If I have a class that looks like:
public class MyClass
{
private string _name = "jeff";
private int _age = 22;
private List<MyClass> _siblings;
}
They all have the correct properties and what not, I'm just to lazy to type
them.
So what I want to do is display property information with a function that
looks similar to:
public void displayProperties(object o)
{
if(o.GetType().IsValueType || o.GetType() == typeof(string))
Console.writeline(o.ToString());
Type myType = o.GetType();
PropertyInfo[] props = myType.GetProperties(..flags...);
foreach(PropertyInfo p in props)
{
console.writeline(p.Name + ": ");
object anotherO = p.GetValue(o,null);
if(anotherO.GetType().IsValueType || anotherO.GetType() ==
typeof(string))
conole.writeline(anotherO.ToString());
else
displayProperties(anotherO); <-- recursion
}
}
Now I really hacked this up to simplify it, but you get the picture.
My method works pretty well for most objects, but I can't loop through
something that looks like MyClass above. because when I get to the
MyClass.Siblings property it just doesn't know what to do...wait....I don't
know what to do.
If the siblings contains two more elements, I just want to loop through
those and call my same function.
Any ideas how I can do this? Same problem for an ArrayList and the like.
Can I do something like " if(o.IsACollectionOfSomeSort)" "foreach(object z
in 'thecollection')displayProperties(z)"
I hope you can at least understand my problem after all this damn typing....