Re: Confused about using Reflection to examine a collection
- From: Robert W. <RobertW@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 13 May 2005 09:38:01 -0700
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." <RobertW@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:F10C5A7F-E25D-4FE7-B844-C29AE3D6635D@xxxxxxxxxxxxxxxx
> > 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
> >
>
>
>
.
- References:
- Confused about using Reflection to examine a collection
- From: Robert W.
- Re: Confused about using Reflection to examine a collection
- From: Bob Powell [MVP]
- Confused about using Reflection to examine a collection
- Prev by Date: Re: Is this good use of Properties?
- Next by Date: Re: How to cast for GetOcx()
- Previous by thread: Re: Confused about using Reflection to examine a collection
- Next by thread: Re: C# - Problem to receive data from a C++ Dll
- Index(es):
Relevant Pages
|