反序列化单例

时间:2014-12-09 01:58:44

标签: c# winforms serialization deserialization

我在这里看到如何序列化单例:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iobjectreference%28v=vs.80%29.aspx

我无法弄清楚如何反序列化它。我知道我需要在构造函数中使用info.GetValue,但我不知道在哪里可以做到这一点。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Permissions;
using System.IO;

namespace Man
{
    [Serializable()]
    public class ListProduct
    {
        public string Name;
        public int Quantity;
        public decimal Cost;
        public DateTime Date;
    }

    [Serializable()]
    public class Product
    {
        public string Name;
        public bool IsCompound;
        public decimal BuyPrice;
        public decimal SellPrice;
        public List<ListProduct> SubItems = new List<ListProduct>();
    }

    [Serializable()]
    public class ListEmployee
    {
        public string FirstName;
        public string LastName;
        public decimal Cost;
        public decimal Hours;
        public DateTime Date;
    }

    [Serializable()]
    public class Employee
    {
        public string FirstName;
        public string LastName;
        public decimal Wage;
    }

    [Serializable()]
    public sealed class Items : ISerializable
    {
        public static List<Product> ProdList = new List<Product>();
        public static List<Employee> EmpList = new List<Employee>();
        public static List<ListProduct> BuyList = new List<ListProduct>();
        public static List<ListProduct> SellList = new List<ListProduct>();
        public static List<ListEmployee> EmpHours = new List<ListEmployee>();
        //private static Items instance = new Items();
        private static Items instance;


        public static Items Instance
        {
            get
            {
                return instance;
            }
        }

        private Items()
        {

        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Instead of serializing this object,  
            // serialize a SingletonSerializationHelp instead.
            info.SetType(typeof(SingletonSerializationHelper));

            info.AddValue("Products", ProdList);
            info.AddValue("BuyList", BuyList);
        }
    }

    [Serializable]
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
    internal sealed class SingletonSerializationHelper : IObjectReference
    {
        // This object has no fields (although it could).

        // GetRealObject is called after this object is deserialized. 
        public Object GetRealObject(StreamingContext context)
        {
            // When deserialiing this object, return a reference to  
            // the Singleton object instead. 
            return Items.Instance;
        }
    }
}

我用这个保存文件:

Stream stream = File.Open(Pathify("testing.bin"), FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(stream, Items.Instance);
stream.Close();

那么如何加载文件并反序列化呢?

0 个答案:

没有答案