Convert List<object> to List<Type>, Type is known at runtime
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
The type is only known at runtime so I guess generic method isn't the way to go
public static object ConvertList(List<object> value, Type type)
{
IList list = (IList)Activator.CreateInstance(type);
foreach (var item in value)
{
list.Add(item);
}
return list;
}
Not sure if this helps at all, but can you use Linq Cast?
List<object> theList = new List<object>(){ 1, 2, 3};
List<int> listAsInt = theList.Cast<int>();