0

Here T could be an array or a single object. How can add the array to an arraylist or add a single object to the same arraylist. This gives me a build-time error that the overloaded match for AddRange has invalid arguments.

T loadedContent;
if (typeof(T).IsArray)
{
    contentArrayList.AddRange(loadedContent);
}
else
{
    contentArrayList.Add(loadedContent);
}
joce
  • 9,624
  • 19
  • 56
  • 74
  • possible duplicate of: http://stackoverflow.com/q/2788636/210709 – IAbstract Apr 27 '11 at 19:03
  • 3
    what is the type of contentArrayList? – Adam Ralph Apr 27 '11 at 19:08
  • 1
    I removed the XNA tag because your question is really about C#. But on the XNA front, it is generally very unusual to need to make a "list" of content that you have loaded. Just let `ContentManager` deal with it. – Andrew Russell Apr 28 '11 at 05:10
  • (And on the C# front: this is a fairly flagrant non-use of the typing system. I could give you an answer of how you could do this... but you probably shouldn't be doing this in the first place.) – Andrew Russell Apr 28 '11 at 05:17

3 Answers3

0

EDIT: Corrected my answer after checking some of the rules around casting to type Array.

All specific types of arrays, such as int[], string[], or MyCustomObject[] derive from the base Array class, and as such, they implement the ICollection interface, which is what the ArrayList.AddRange method accepts as a parameter.

Assuming that your contentArrayList variable is an ArrayList object, you should be able to cast your loadedContent variable to ICollection:

contentArrayList.AddRange((ICollection)loadedContent)

Alternatively, you could combine the check for whether it is an array with the cast:

Array loadedContentAsArray = loadedContent as Array;
if (loadedContentAsArray != null)
{
    contentArrayList.AddRange(loadedContentAsArray);
}

Dr. Wily's Apprentice
  • 10,212
  • 1
  • 25
  • 27
  • Indeed you could use a [Type Constraint](http://msdn.microsoft.com/en-us/library/d5x73970.aspx) on the `T` parameter to ensure it implements `ICollection` Example: `where T : ICollection` – Adrian Clark Apr 28 '11 at 05:15
0

The solution provided by Dr. Wily's Apprentice will work but I would like to make some side comments.

You must have design issue in your code if you use generics but you are still committed to a specific data type. Basically you destroy the purpose of the generics, as stated on MSDN:

Generics allow you to define type-safe data structures, without committing to actual data types.

Maybe you should reconsider some refactoring, maybe by adding methods with different parameters or something...

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • I'm not commited to a specific type, but rather either an array of objects or a single object. This is because LoadContent returns either of these depending on the parameter (single or []) – user727904 Apr 28 '11 at 22:37
0

If your contentList is of Type ArrayList, I would go that way.

        ICollection contentArray = loadedContent as ICollection;
        if (contentArray != null)
            contentList.AddRange(contentArray);
        else
            contentList.Add(loadedContent);
BitKFu
  • 3,649
  • 3
  • 28
  • 43