471,350 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Reflection and Generics, Collections, etc...

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....
Jan 13 '06 #1
3 1340

INeedADip wrote:

<snip>
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....


Well, you can do:

if (o is IEnumable)
{
foreach (object element in (IEnumerable)o)
{
...
}
}

Note that there are things which implement IEnumerable which you
wouldn't just want to enumerate - and there may be other properties
you're interested in, too.

I suspect you'll find it hard to write a robust version of this which
always does what you want it to, to be honest.

Jon

Jan 13 '06 #2
How about:

IEnumerable e = anotherO as IEnumerable;
if (e != null) {
foreach (object enumItem in e) {
displayProperties(enumItem );
}
}

Haven't tested it, but...?

Also - you should discard null values of anotherO before you call GetType()
or "is"

Marc

anotherO
"INeedADip" <IN*******@gmail.com> wrote in message
news:uE*************@tk2msftngp13.phx.gbl...
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....

Jan 13 '06 #3
This worked perfectly....
Thank you.
Jan 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Marc | last post: by
1 post views Thread by Rathish P S | last post: by
1 post views Thread by uttara | last post: by
2 posts views Thread by Wiktor Zychla [C# MVP] | last post: by
6 posts views Thread by nick_nw | last post: by
reply views Thread by Konrad Kaczanowski | last post: by
3 posts views Thread by Showjumper | last post: by
4 posts views Thread by SHEBERT | last post: by
7 posts views Thread by SpotNet | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.