Reading objects attributes of an Arraylist

  • Thread starter Thread starter ree
  • Start date Start date
R

ree

I have an arraylist and I want to read attributes of the object in it. But
with C++ using vectors its was straight forward. But I am lost with
arraylists as these can hold variety of objects.
 
Hi ree,

Yes, an ArrayList will return objects so you need to cast the object back to it's original class or one of it's "parents" or interfaces to be able to access any properties.

You can test if an object can be cast to a class like this

object obj = list[0];

if(obj is MyClass)

And get the real type of the object with

Type t = obj.GetType();

Happy coding!
Morten Wennevik [C# MVP]
 
Ah, upon rereading, I reliazed you asked about attributes, not properties.
You can try using the Type's Attributes property. Not exactly sure how to use it, but testing it on a string and converting the TypeAttribute using ToString() gave me

AutoLayout, AnsiClass, NotPublic, Public, Sealed, Serializable, BeforeFieldInit

Happy coding!
Morten Wennevik [C# MVP]
 

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

Back
Top