How to Deserialize using binary Deserialization from file text file

前端 未结 1 1902
眼角桃花
眼角桃花 2021-01-27 19:23
public static List LoadRestaurantList() 
{
    FileStream fs = new FileStream(\"Restaurant.txt\", FileMode.OpenOrCreate); 
    BinaryFormatter bf = new         


        
相关标签:
1条回答
  • 2021-01-27 19:49

    Serialization and Deserialization are each others opposites. This means the type(s) used during serialization needs to be the same during deserialization.

    In your code that is not the case. You serialize Restaurant types but when you deserialize you expect a List.

    Adapt your serialization code as follows:

    public static void SaveRestaurantList(List<Restaurant> restaurantList) 
    { 
       using(FileStream fs = new FileStream("Restaurant.txt", FileMode.Create, FileAccess.Write))
       {
           BinaryFormatter bf = new BinaryFormatter(); 
           bf.Serialize(fs, restaurantList); 
       }
    }
    
    0 讨论(0)
提交回复
热议问题