When is an array not an array?

  • Thread starter Thread starter Jerry Negrelli
  • Start date Start date
J

Jerry Negrelli

I was enumerating a collection and printing the value of
each data item when I noticed that some of the objects in
the collection returned "System.Object[]" from
their .ToString() method.

Assuming that they aren't literally strings with the
value "System.Object[]", how can I convert one of these
objects to an object array? Reflection?
Convert.ChangeType?

My code:

DirectoryEntry parent = new DirectoryEntry
(@"LDAP://RootDSE");

IEnumerator bob =
parent.Properties.PropertyNames.GetEnumerator();
while(bob.MoveNext()){
if(parent.Properties[bob.Current.ToString
()].Value.GetType().IsArray){
Response.Write("my array: " + parent.Properties
[bob.Current.ToString()].Value.ToString());
}
}

JER
 
Jerry Negrelli said:
I was enumerating a collection and printing the value of
each data item when I noticed that some of the objects in
the collection returned "System.Object[]" from
their .ToString() method.

Assuming that they aren't literally strings with the
value "System.Object[]", how can I convert one of these
objects to an object array? Reflection?
Convert.ChangeType?

Just cast to object[]. You don't need to convert the objects at all -
they *are* arrays.
 
I'd tried that but was getting an invalid cast exception
at runtime. It turns out there is also a byte array in
the collection, so casting the byte array to an object
array is what was causing the error.

Thanks Jon!

Jerry
-----Original Message-----
Jerry Negrelli
I was enumerating a collection and printing the value of
each data item when I noticed that some of the objects in
the collection returned "System.Object[]" from
their .ToString() method.

Assuming that they aren't literally strings with the
value "System.Object[]", how can I convert one of these
objects to an object array? Reflection?
Convert.ChangeType?

Just cast to object[]. You don't need to convert the objects at all -
they *are* arrays.

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.
 
Back
Top