473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1396

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Helium | last post by:
Is there any need for "System.Collections.Queue" any more? OK, .Net made the mistake to start without generics, but that is fixed now. Languages without support for generics could use...
2
by: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a...
1
by: Rathish P S | last post by:
Hi friends, I am trying with Reflection and Generics in C#. But I get confused with some of these new features. I am trying to get the type of generic parameter with the...
1
by: uttara | last post by:
I have a generic collection which I am using in classes to store a collection of embedded objects. Class Employee: IEntity { Private string mName; Private int mEmployeeID; …. Private...
2
by: Wiktor Zychla [C# MVP] | last post by:
Could anyone confirm/deny that following is a bug (or at least an "unexpected behaviour")? If this is not a bug, I would be glad for a short explanation or a workaround. Issue: A generic...
6
by: nick_nw | last post by:
Hi, What significant advantages do generics give me over passing objects around as 'object' and casting when needed? I was asked this recently and started off by saying, "well of course...
0
by: Konrad Kaczanowski | last post by:
Hi all, I'm creating code generator for wrappers of some c# classes. With the introduction of c# 2.0 and generics the following problem arises. When encountering generic types anywhere inside the...
3
by: Showjumper | last post by:
Back in asp.net 1.1 i made custom collection classes per Karl Seguin's article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes to take advantage of strongly typed data. Now with...
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.