C#带有列表的类的部分二进制反序列化

时间:2014-01-13 15:18:54

标签: c# list class serialization binary

友好的问候!

由于这种方法的速度,我开始使用二进制序列化来保存/加载数据 我为培训目的创建了一个小课程,它完美无缺!

[Serializable()]
    public class School : ISerializable
    {
        internal List<int> Boards, Tables, Children;
        public School()
        {
            this.Boards = new List<int>();
            this.Tables = new List<int>();
            this.Children = new List<int>();
        }
        public School(SerializationInfo info, StreamingContext context)
        {
            this.Boards = (List<int>)info.GetValue("Boards", typeof(List<int>));
            this.Tables = (List<int>)info.GetValue("Tables", typeof(List<int>));
            this.Children = (List<int>)info.GetValue("Children", typeof(List<int>));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Boards", Boards);
            info.AddValue("Tables", Tables);
            info.AddValue("Children", Children);
        }
        public void FillRandom()
        {
            for (int a = 0; a < 1000; a++)
                this.Boards.Add(a);
            for (int b = 0; b < 1000; b++)
                this.Tables.Add(b);
            for (int c = 0; c < 1000; c++)
                this.Children.Add(c);
        }
        /// <summary>
        /// Saves the current state of this class to a serialized data file.
        /// </summary>
        /// <param name="path">File location</param>
        public void Serialize(string path)
        {
            using (FileStream FS = File.Open(path, FileMode.Create))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(FS, this);
                FS.Close();
            }
        }

        /// <summary>
        /// Deserializes the data file and builds a School class from the retrieved information.
        /// </summary>
        /// <param name="path">File location</param>
        /// <returns>Filled People class</returns>
        public static People Deserialize(string path)
        {
            using (FileStream FS = File.Open(path, FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                People p = (People)bf.Deserialize(FS);
                FS.Close();
                return p;
            }
        }
    }


对于大量的代码我很抱歉
这是我的问题
由于可能存储在此文件中的大量列表,我需要检索List的一部分。当多个用户访问服务器时,它主要是一种内存使用预防措施,以防止它填满...
我无法弄清楚如何处理这个问题,我很感激那些在这个问题上有更多专业知识的人的帮助。

谢谢你的时间!

0 个答案:

没有答案