By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

Confused about using Reflection to examine a collection

P: n/a
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[i].FieldType;
object obj = fields[i].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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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." <Ro*****@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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[i].FieldType;
object obj = fields[i].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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #2

P: n/a
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 [MVP]" wrote:
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." <Ro*****@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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[i].FieldType;
object obj = fields[i].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?

--
Robert W.
Vancouver, BC
www.mwtech.com


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.