Re: Confused about using Reflection to examine a collection



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
> >
>
>
>
.



Relevant Pages

  • Re: Problems with (Cast)
    ... >> Why I'm getting this error when I try to cast as char? ... I wouldn't expect a single-character string to cast to ... > type of obj if it still doesn't work: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: cast ?
    ... Is it possible to cast some object using the name of its type as a string ... realize that a cast may not be what you want at all. ... All a cast does is tell the _compiler_ that you expect something to be ... Reflection, which is an entirely run-time construct: ...
    (microsoft.public.dotnet.csharp.general)
  • How to cast to a dynamic type ?
    ... I am confused how to cast to a type which is obtained from reflection: ... using ABC; ... if (obj is ABC.MyClass) ...
    (microsoft.public.dotnet.framework)
  • Re: Knowing an object implements an Interface
    ... (without casting or anything like that). ... Something that uses reflection? ... cast to the given type: ... 'obj is IComparable' will evaluate to true if obj can be cast to an ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problems with (Cast)
    ... > Why I'm getting this error when I try to cast as char? ... I wouldn't expect a single-character string to cast to ... type of obj if it still doesn't work: ...
    (microsoft.public.dotnet.languages.csharp)