Unbox an Object to Its Type

Posted by Ngu Soon Hui on Stack Overflow See other posts from Stack Overflow or by Ngu Soon Hui
Published on 2010-03-22T07:51:22Z Indexed on 2010/03/22 8:01 UTC
Read the original article Hit count: 421

Filed under:

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?

© Stack Overflow or respective owner

Related posts about c#