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

How can I Convert List<?> to a System.Array

P: n/a
I am trying to use reflection to output the fields (names and values) of an
arbitrary object -- an object dump to a TreeView.

It works pretty well, but I am having trouble with generic lists, like
List<char>.

What I have working is :

Type type = newObj.GetType(); //newObj is what I'm trying to dump

fieldInfo = new List<ObjFields>(200); //text representation of
the fields

//code to get info of a generic list:
if (type.Namespace == "System.Collections.Generic")
{
FieldInfo items =
type.GetField("_items",
BindingFlags.Instance |
BindingFlags.NonPublic );

if (items != null)
{
System.Array array =
items.GetValue(newObj) as System.Array;
if (array != null)
{
for (int i = 0; i < array.Length; i++)
{
//get the value and name
fieldInfo.Add(new ObjFields(
array.GetValue(i), //field value
"[" + i.ToString() + "]")); //field "name"
}
return;
}
}

which works fine. However, I am getting the collection by keying on
the name "_items" (in Type.GetField), which is dangerous since MS might
change the internals of the generic list.

What I'd like to do is cast the generic list to something like an
Object[], or System.Array, or convert it to such, without compiling in the
types of the members of the generic list.

I.E. how can I go from a List<to an Object[]/Array without knowing the
types of the elements of the generic list?

Clearly,

Array newArray = newObj as System.Array; //where
newObj is of type List<?>

does not work, but that's the effect I am after.

--
Fred Mellender, Rochester, NY
Jun 28 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sat, 28 Jun 2008 16:11:50 -0700, Fred Mellender
<no******************@gmail.comwrote:
[...]
I.E. how can I go from a List<to an Object[]/Array without knowing the
types of the elements of the generic list?
Without knowing more about your larger design, it's hard to know just
where you expect to special case things. But it seems like a collection
special case is clearly something you're after. So perhaps something like
this:

object[] rgobj = null;

if (newObj is IEnumerable)
{
rgobj = ((IEnumerable)newObj).Cast<object>().ToArray();
}

It seems to me that you should be able to avoid all the reflection stuff,
and the above will work with any collection that implements IEnumerable
(in other words, basically every single one of them :) ), not just List<T>.

Note that the above uses LINQ extension methods. If you're not using the
latest version of .NET, then you'll have to write the Cast() and ToArray()
methods yourself. But that would be fairly simple to do.

Pete
Jun 28 '08 #2

P: n/a
Fred Mellender <no******************@gmail.comwrote:

<snip>
I.E. how can I go from a List<to an Object[]/Array without knowing the
types of the elements of the generic list?
Call the ToArray method on it.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 29 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.