Unbox an Object to Its Type
- by Ngu Soon Hui
Hello, is there anyway to unbox an object to its real type?
Basically I am given an ArrayList, the array list are actually a list of int or double, or maybe other types ( it can be either, but it is either all int or double, no mix). Now, I will have to return a List<double> or List<int> or other list, depending on what is the real type.
public List<T> ConvertToList<T>(ArrayList arr)
{
var list1 = new List<T>();
foreach(var obj in arr)
{
// how to do the conversion?
var objT = ??
list1.Add(objT);
}
return list1;
}
Any idea?