472,958 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1386

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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.