Convert array of any type to List<T> (C#)
<p>The function below accepts an object, which can sometimes be an array of a given type. In this case, I suppose the type could be determined with obj[0].GetType(), assuming the array has at least one member. I would like to convert such an array to a generic List&lt;T&gt; of appropriate type, but the code below only succeeds in converting to List&lt;object&gt;. How can this be done?</p>

<pre>
        public object GetDeserializedObject(object obj, Type targetType)
        {
            if (obj is Array)
            {
                List&lt;object&gt; obj2 = new List&lt;object&gt;();
                for (int i = 0; i &lt; ((Array)obj).Length; i++)
                {
                    obj2.Add(((object[])obj)[i]);
                }
                obj = obj2;
            }
            return obj;
        }
</pre>

<p>Note that GetSerializedObject() implements a function belonging to the IDataContractSurrogate interface, so I don't think I can change its signature as shown.</p>
