Deserialize <Object> List C #

I want to assign List<Object>

I get a new from this methodList<MyClass>

    public List<Object> Deserialize(string path)
    {
        IFormatter seri = new BinaryFormatter();

        Stream str = new FileStream(path, FileMode.Open, FileAccess.Read);
        List<Object> Lista = (List<Object>)(seri.Deserialize(str));
        str.Close();
        return Lista;
    }

      

My intention is to call this method with different classes and then revert it back to the previous type, but I am getting an error trying to revert it back.

+3


source to share


2 answers


You can try using Generics

:



public List<T> Deserialize<T>(string path)
{
    IFormatter seri = new BinaryFormatter();

    Stream str = new FileStream(path, FileMode.Open, FileAccess.Read);
    List<T> Lista = (List<T>)(seri.Deserialize(str));
    str.Close();
    return Lista;
}

      

+2


source


public List<T> Deserialize<T>(string path)
{
   return JsonConvert.DeserializeObject<List<T>>(path);
}

      



0


source







All Articles