c# - 如何反序列化然后将数据放入表格?

标签 c# serialization deserialization generic-list binaryformatter

目标

我目前有一个保存信息的有效方法

然后我希望程序在退出后单击“加载”按钮时加载保存的状态。

然后我希望程序在适当的地方显示数据

在表单中,我有两个 DataGridView,一个用于员工,另一个用于主管。

============================================= ==============

方法

我已经将两个通用列表序列化为一个 .dat 文件

BinaryFormatter bFormatter = new BinaryFormatter();    
using (FileStream fs = new FileStream(FILENAME, FileMode.OpenOrCreate))
{
  bFormatter.Serialize(fs, employees);
  bFormatter.Serialize(fs, supervisors);
}

到目前为止,在 .dat 文件中,我有 3 名员工和 2 名主管,我不确定如何提取信息并将它们放入适当的位置

名单如下:

List<Employee> employees = new List<Employee>();
List<Supervisor> supervisors = new List<Supervisor>();

Employee e1 = new Employee(MemberJob.Employee, "Name", MemberSkills.CPlus | MemberSkills.CSharp, false);
Supervisor s1 = new Supervisor(MemberJob.Supervisor, "Another name", false); 
employees.Add(e1);
supervisors.Add(s1);

============================================= ==========================

尝试

我在互联网和 Stackoverflow 上进行了广泛的浏览,但主要是它与我的上下文无关,或者它们使用的是我不想使用的 XML 格式。

我假设这只是复制 serialize 方法并将 bFormatter.Serialize(fs, employees); 更改为 bFormatter.Deserialize (fs, employees); 但在反序列化列表后我不知道该怎么做。

 BinaryFormatter bFormatter = new BinaryFormatter();
 FileStream fs = File.Open(FILENAME, FileMode.Open);
 object obj = bFormatter.Deserialize(fs);

然后该对象带回我需要的数据,但我无法将 object obj 数据放入可用格式,如果可能的话它会卡在 object obj 中我'我想尝试将其放回 Employee list

最佳答案

送给你的圣诞礼物。希望能帮助到你。 :)

namespace WpfApplication3
{
    public partial class App : Application
    {
        string path = @"C:\Users\xxx\Desktop\myFile.dat";

        public App()
        {
            InitializeComponent();

            //Test
            List<Employee> eList = new List<Employee>();
            eList.Add(new Employee("aaa"));
            eList.Add(new Employee("bbb"));

            List<Supervisor> sList = new List<Supervisor>();
            sList.Add(new Supervisor("ccc"));
            sList.Add(new Supervisor("ddd"));

            SavedInfo savedInfo = new SavedInfo();
            savedInfo.employeeList = eList;
            savedInfo.supervisorList = sList;


            SaveToFile(savedInfo); //Save to file

            SavedInfo newSaveGame = LoadFromFile(); //Load from file

            foreach (var e in newSaveGame.employeeList)
                Console.WriteLine("Employee: " + e.name);

            foreach (var e in newSaveGame.supervisorList)
                Console.WriteLine("Supervisor: " + e.name);
        }

        public void SaveToFile(SavedInfo objectToSerialize)
        {
            Stream stream = File.Open(path, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, objectToSerialize);
            stream.Close();
        }

        public SavedInfo LoadFromFile()
        {
            if (!System.IO.File.Exists(path))
                return new SavedInfo();

            SavedInfo objectToSerialize;
            Stream stream = File.Open(path, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            objectToSerialize = (SavedInfo)bFormatter.Deserialize(stream);
            stream.Close();
            return objectToSerialize;
        }
    }

    [Serializable()]
    public class SavedInfo
    {
        public List<Employee> employeeList = new List<Employee>();
        public List<Supervisor> supervisorList = new List<Supervisor>();
    }

    [Serializable()]
    public class Employee
    {
        public string name = "";

        public Employee(string eName)
        {
            name = eName;
        }
    }

    [Serializable()]
    public class Supervisor
    {
        public string name = "";

        public Supervisor(string eName)
        {
            name = eName;
        }
    }
}

编辑:根据 jdweng 的评论进行编辑。我认为 jdweng 是对的。

关于c# - 如何反序列化然后将数据放入表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459138/

相关文章:

c# - Word 文档中的 Excel 范围在更新链接后不保留格式

java - 包含 Map<String, Object> 字段的对象的 Jackson 默认类型

java - 将不同格式的JSON字符串反序列化为同一个Java类的实例

c# - XML 反序列化 C#

c# - IEnumerable 类中的委托(delegate)列表<T>

c# - 我怎样才能在这里使用通用方法?

java - 无法将消息发送到远程对等本地类不兼容 : stream classdesc serialVersionUID

python - Objective C 到 Python 序列化

c# - 列表的 XML 序列化变得太大

c# - 特定 Controller 的操作列表