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<T> of appropriate type, but the code below only succeeds in converting to List<object>. How can this be done?
public object GetDeserializedObject(object obj, Type targetType)
{
if (obj is Array)
{
List<object> obj2 = new List<object>();
for (int i = 0; i < ((Array)obj).Length; i++)
{
obj2.Add(((object[])obj)[i]);
}
obj = obj2;
}
return obj;
}
Note that GetSerializedObject() implements a function belonging to the IDataContractSurrogate interface, so I don't think I can change its signature as shown.