I believe what you want is:
public static object ConvertList(List<object> value, Type type)
{
var containedType = type.GenericTypeArguments.First();
return value.Select(item => Convert.ChangeType(item, containedType)).ToList();
}
Example usage:
var objects = new List<Object> { 1, 2, 3, 4 };
ConvertList(objects, typeof(List<int>)).Dump();
I'm not sure how useful this is though... It highlights the insanely useful Convert.ChangeType method I guess!
Update: Since others have correctly pointed out that this doesn't actually return a List<T>
(where T is the type in question) and therefore might not fully answer the question at hand, I have chosen to provide a more up to date answer:
public static object ConvertList(List<object> items, Type type, bool performConversion = false)
{
var containedType = type.GenericTypeArguments.First();
var enumerableType = typeof(System.Linq.Enumerable);
var castMethod = enumerableType.GetMethod(nameof(System.Linq.Enumerable.Cast)).MakeGenericMethod(containedType);
var toListMethod = enumerableType.GetMethod(nameof(System.Linq.Enumerable.ToList)).MakeGenericMethod(containedType);
IEnumerable<object> itemsToCast;
if(performConversion)
{
itemsToCast = items.Select(item => Convert.ChangeType(item, containedType));
}
else
{
itemsToCast = items;
}
var castedItems = castMethod.Invoke(null, new[] { itemsToCast });
return toListMethod.Invoke(null, new[] { castedItems });
}
If you don't need the conversion (so the type of each value is actually correct, and you don't have ints in strings etc), then remove the performConversion
flag and the associated block.
Example: https://dotnetfiddle.net/nSFq22
List<object>
. See: dotnetfiddle.net/Hd4y1c – Piassava