简体   繁体   English

如何将对象反序列化为通用列表?

[英]How do I deserialize an object into a generic list?

I have the following deserialization function. 我具有以下反序列化功能。

private object Deserialize(string file)
{
   var ret = new object(); 
   var fmt = new BinaryFormatter(); 
   using(FileStream fs = File.Open(file, FileMode.Open)) 
   {
      ret = fmt.Deserialize(fs); 
   } 

   return ret; 
} 

I call the function and cast it as a generic list of the expected type. 我调用该函数并将其转换为期望类型的通用列表。

var list = Deserialize(file) as List<Something>;

But I get a null value. 但是我得到一个null值。 Debugging the code shows that the function works. 调试代码表明该功能有效。 The result object is a list. result对象是一个列表。

For one, File.Open does not have an overload that takes a single argument. 首先, File.Open没有使用单个参数的重载。 At the very minimum, you need to specify the FileMode as well. 至少,您还需要指定FileMode。

FileStream fs = File.Open(file,FileMode.Open)

Secondly, Filestream does not have a member method Deserialize , so fs.Deserialize is invalid. 其次, Filestream没有成员方法Deserialize ,因此fs.Deserialize无效。

To help you further, what kind of a file are you attempting to deserialize. 为了进一步帮助您,您尝试反序列化哪种文件。 CSV, XML... CSV,XML ...

List<Something> ret;
ret=(List<Something>)serializer.fmt(fs); 

I wrote a program that serializes/deserializes some data based on the code you provided , and it works fine. 我编写了一个程序,可以根据您提供的代码对一些数据进行序列化/反序列化,并且可以正常工作。

You said: 你说:

But I get a null value. 但是我得到一个空值。 Debugging the code shows that the function works. 调试代码表明该功能有效。 The result object is a list. 结果对象是一个列表。

There is a subtle difference between 2 casting operations in C# C#中的2个转换操作之间有细微的差别

string text = (string)GetString(); // throws exception if cannot be cast
string text = GetString() as string; // returns null if source cannot be cast

Other than that, based on the limited amount of info that you provided, we won't be able to help you further troubleshoot. 除此之外,根据您提供的有限信息,我们将无法帮助您进行进一步的故障排除。

Can you narrow it down to the line of code that isn't working? 您可以将其范围缩小到无效的代码行吗? Are you sure that the serialization is working properly? 您确定序列化工作正常吗?

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Cereal
{
  class Program
  {
    static void Main(string[] args)
    {
      string file = Path.Combine(Environment.CurrentDirectory, "cereal.bin");

      List<string> namesToWrite = new List<string>();
      namesToWrite.Add("Frosted Flakes");
      namesToWrite.Add("Fruit Loops");
      namesToWrite.Add("Lucky Charms");
      namesToWrite.Add("Apple Jacks");

      Serialize(file, namesToWrite);

      List<string> namesToRead = Deserialize(file) as List<string>;;

      foreach (string name in namesToRead)
      {
        Console.WriteLine(name);
      }

      Console.ReadLine();
    }

    static void Serialize(string file, object stuff)
    {
      var fmt = new BinaryFormatter();

      using (FileStream fs = File.Open(file, FileMode.Create))
      {
        fmt.Serialize(fs, stuff);
      }
    }

    static object Deserialize(string file)
    {
      var ret = new object();
      var fmt = new BinaryFormatter();

      using (FileStream fs = File.Open(file, FileMode.Open))
      {
        ret = fmt.Deserialize(fs);
      }

      return ret;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM