472,952 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 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

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 =
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"

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?


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 6000
On Sat, 28 Jun 2008 16:11:50 -0700, Fred Mellender
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

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.

Jun 28 '08 #2
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?
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Janus Knudsen | last post by:
Hello Im getting an error when I try to convert like this: string arrDSN; objDSN.GetDataSourceList(arrDSN) And the error: Argument '1': cannot convert from 'string' to 'ref System.Array'
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
by: klynn | last post by:
I defined a session variable as an array using Session = new string; Later, in my code, I need to set it and sort it. I tried Session = "some string"; My error is "Cannot apply indexing with to...
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a...
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse...
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
by: Peter | last post by:
I have a WebService which returns a List of RunningReport class How do I read this XML data on the client side. How do I convert List<RunningReportfrom the WebService side to List<RunningReporton...
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've an optimization question for you all really quick. I have a stream that I am reading some bytes. At times, the stream can contain a small amount of bytes such as 50...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.