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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred Mellender

    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.Collect ions.Generic")
    {
    FieldInfo items =
    type.GetField(" _items",
    BindingFlags.In stance |
    BindingFlags.No nPublic );

    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(n ew 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


  • Peter Duniho

    #2
    Re: How can I Convert List&lt;?&gt; to a System.Array

    On Sat, 28 Jun 2008 16:11:50 -0700, Fred Mellender
    <nospamPlease_f redm73@gmail.co mwrote:
    [...]
    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)n ewObj).Cast<obj ect>().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

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: How can I Convert List&lt;?&gt; to a System.Array

      Fred Mellender <nospamPlease_f redm73@gmail.co mwrote:

      <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 - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon_skeet
      C# in Depth: http://csharpindepth.com

      Comment

      Working...