以下の実装でこれを行うことができます。
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]);
}
}
実装:
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;
}
}