Confused about using Reflection to examine a collection

G

Guest

I'm trying to write a utility that will use Reflection to examine any data
model I pass it and correctly map out this model into a tree structure. When
I say "any" [data model], in fact there will only be 3 types of items in the
very hierarchical data model:
- Classes (and nested classes)
- Collections
- Properties

I've successfully written the Reflection code to handle any combination of
classes and properties but I'm confused about what to do when I encounter a
collection. Here's some starting code I've written:

-----------------------------------------------------------------------------
Type modelType = model.GetType();
FieldInfo[] fields = modelType.GetFields();

for(int i = 0; i < fields.Length; i++)
{
Type fieldType = fields.FieldType;
object obj = fields.GetValue(model);
}
-----------------------------------------------------------------------------

Remember that I don't know ahead of time what types of collections I'll be
encountering. So though the 'fieldType' variable seems to correctly identify
the type of the collection, I can't figure out how to cast 'obj' to its true
type ... and then from there I could examine the elements that populate the
collection.

So I'm wondering two things:
1. Is what I'm asking to do possible?
2. Am I close or barking up the wrong tree?
 
B

Bob Powell [MVP]

At their basic level a collection will implement IEnumerable and you'll be
able to iterate over the objects in the collection. As soon as you have an
object from that collection you can use reflection to ascertain it's real
type and extract methods and properties from it.

Get whatever object it is and see if it casts to IEnumerable. if it does,
it's a collection and you can use foreach(object o in blahblah) to get at
the items in it.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

Bob,

Maybe I'm just dense or perhaps I haven't had enough coffee ... but its the
casting to the desired type that I can't figure out.

For example, in my sample code, I need to figure out how to cast 'obj' to
the precise type that Reflection is saying it is. For argument's sake, let's
say that 'obj' is actually a custom collection (built from CollectionBase).
Let's say it type name is "ComplexClass".

If it were a basic type, such as 'string' then I would do a cast like this:

string str = obj as string;

OR:

string str = (string) obj;


BUT I can't do this with my situation because I have only a reference to
'ComplexClass', not {ComplexClass} itself. Do you see the difference?

Your suggestion about using a foreach loop and making each element an object
doesn't appear to work.

Where am I going wrong?

Robert



Bob Powell said:
At their basic level a collection will implement IEnumerable and you'll be
able to iterate over the objects in the collection. As soon as you have an
object from that collection you can use reflection to ascertain it's real
type and extract methods and properties from it.

Get whatever object it is and see if it casts to IEnumerable. if it does,
it's a collection and you can use foreach(object o in blahblah) to get at
the items in it.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Robert W. said:
I'm trying to write a utility that will use Reflection to examine any data
model I pass it and correctly map out this model into a tree structure.
When
I say "any" [data model], in fact there will only be 3 types of items in
the
very hierarchical data model:
- Classes (and nested classes)
- Collections
- Properties

I've successfully written the Reflection code to handle any combination of
classes and properties but I'm confused about what to do when I encounter
a
collection. Here's some starting code I've written:

-----------------------------------------------------------------------------
Type modelType = model.GetType();
FieldInfo[] fields = modelType.GetFields();

for(int i = 0; i < fields.Length; i++)
{
Type fieldType = fields.FieldType;
object obj = fields.GetValue(model);
}
-----------------------------------------------------------------------------

Remember that I don't know ahead of time what types of collections I'll be
encountering. So though the 'fieldType' variable seems to correctly
identify
the type of the collection, I can't figure out how to cast 'obj' to its
true
type ... and then from there I could examine the elements that populate
the
collection.

So I'm wondering two things:
1. Is what I'm asking to do possible?
2. Am I close or barking up the wrong tree?

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top