Reputation: 51
I have a collection of type List that I want to convert to SomeType[]. SomeType is not known before runtime.
This must be done with the signature of the following procedure.
private object ConvertListToArray(IList collection)
{
// This does not work since SomeType is not known before runtime.
var convertedList = collection.Cast<SomeType>().ToArray();
return convertedList;
}
Notice that collection is IList, but it is known that the concrete type is
List<SomeType>
The return collection must be an object of type SomeType[].
How can this be done?
Upvotes: 4
Views: 3606
Reputation: 8656
I'm posting this as the other answers seem a bit verbose. I'm pretty sure this does what the OP is looking for. It worked for me.
public static Array ConvertToArray(ICollection collection, Type type)
{
var array = Array.CreateInstance(type, collection.Count);
collection.CopyTo(array, 0);
return array;
}
Upvotes: 0
Reputation: 101150
You can do this with the implementation below:
class Program
{
static void Main(string[] args)
{
// same type
var myCollection = new List<string> {"Hello", "World"};
var array = (string[])myCollection.ConvertToArray();
Console.WriteLine(array[0]);
// new type
var intList = new List<int> {1, 2, 3};
var stringArray = (string[])intList.ConvertToArray(typeof(string));
Console.WriteLine(stringArray[0]);
// mixed types
var ouch = new List<object> {1, "Mamma", 3.0};
var result= (string[])ouch.ConvertToArray(typeof(string));
Console.WriteLine(result[0]);
}
}
The implementation:
public static class ListExtensions
{
public static object ConvertToArray(this IList collection)
{
// guess type
Type type;
if (collection.GetType().IsGenericType && collection.GetType().GetGenericArguments().Length == 0)
type = collection.GetType().GetGenericArguments()[0];
else if (collection.Count > 0)
type = collection[0].GetType();
else
throw new NotSupportedException("Failed to identify collection type for: " + collection.GetType());
var array = (object[])Array.CreateInstance(type, collection.Count);
for (int i = 0; i < array.Length; ++i)
array[i] = collection[i];
return array;
}
public static object ConvertToArray(this IList collection, Type arrayType)
{
var array = (object[])Array.CreateInstance(arrayType, collection.Count);
for (int i = 0; i < array.Length; ++i)
{
var obj = collection[i];
// if it's not castable, try to convert it
if (!arrayType.IsInstanceOfType(obj))
obj = Convert.ChangeType(obj, arrayType);
array[i] = obj;
}
return array;
}
}
Upvotes: 1
Reputation: 6401
public static class ListExtensions
{
public static T[] ConvertToArray<T>(IList list)
{
return list.Cast<T>().ToArray();
}
public static object[] ConvertToArrayRuntime(IList list, Type elementType)
{
var convertMethod = typeof(ListExtensions).GetMethod("ConvertToArray", BindingFlags.Static | BindingFlags.Public, null, new [] { typeof(IList)}, null);
var genericMethod = convertMethod.MakeGenericMethod(elementType);
return (object[])genericMethod.Invoke(null, new object[] {list});
}
}
[TestFixture]
public class ExtensionTest
{
[Test]
public void TestThing()
{
IList list = new List<string>();
list.Add("hello");
list.Add("world");
var myArray = ListExtensions.ConvertToArrayRuntime(list, typeof (string));
Assert.IsTrue(myArray is string[]);
}
}
Upvotes: 1
Reputation: 33
You could try:
private T[] ConvertListToArray<T>(IList collection)
{
// This does not work since SomeType is not known before runtime.
var convertedList = collection.Cast<T>().ToArray();
return convertedList;
}
Upvotes: 0