471,319 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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

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

Similar topics

4 posts views Thread by Janus Knudsen | last post: by
3 posts views Thread by klynn | last post: by
reply views Thread by Iron Moped | last post: by
35 posts views Thread by Lee Crabtree | last post: by
6 posts views Thread by Peter | last post: by
9 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.