• .NET面试基础知识之序列化(Serialize)(三)


    Serializing a Type as a different type and deserializing an object as a different object

    下面的代码是序列化一个Singleton类并反序列化

      [Serializable]
        public sealed class SingletonSerializable:ISerializable
        {
            private SingletonSerializable()
            {
            }

            private static readonly SingletonSerializable theOneObject=new SingletonSerializable();

            public string Name="Jeff";
            public DateTime Date = DateTime.Now;

            public static SingletonSerializable GetSingleton()
            {
                return theOneObject;
            }

            [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
            void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.SetType(typeof(SingletonSerializationHelper));

           //Tell the formatter to serialize the SingletonSerializable object as a SingletonSerializableHelper object instead.

          //The formatter automatically detected that both array elements refer to a single object, the formatter serialize only one object.

          //So the formatter no need the special constructor
            }

            [Serializable]

         //When a type implements IObjectReference, the formatter calls the GetRealObject method
            private sealed class SingletonSerializationHelper:IObjectReference
            {
                public Object GetRealObject(StreamingContext context)
                {
                    return SingletonSerializable.GetSingleton();

              //Return a reference to the object that you really want a reference to now that deserialization of the object has completed.
                }
            }
        }

    test

      public static void TestSingleton()
        {
            SingletonSerializable[] al = { SingletonSerializable.GetSingleton(),SingletonSerializable.GetSingleton()};
            Console.WriteLine("Do both elements refer to the same object?"+(al[0]==al[1]));//True

            using (Stream st = new MemoryStream()) {
               BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(st,al);
                st.Position = 0;
                SingletonSerializable[] a2 =(SingletonSerializable[]) formatter.Deserialize(st);
                Console.WriteLine("Do both elements refer to the same object?"+(a2[0]==a2[1]));//True
                Console.WriteLine("Do both elements refer to the same object?"+(al[0]==a2[0]));//True
            }
        }

  • 相关阅读:
    浅析Scrapy框架运行的基本流程
    排序和搜索
    设计模式:桥接模式及代码示例、桥接模式在jdbc中的体现、注意事项
    设计模式:适配器模式(类适配器、对象适配器、接口适配器)
    设计模式:建造者模式及在jdk中的体现,建造者模式和工厂模式区别
    java的线程、创建线程的 3 种方式、静态代理模式、Lambda表达式简化线程
    设计模式:原型模式介绍 && 原型模式的深拷贝问题
    设计模式:工厂设计模式介绍及3种写法(简单工厂、工厂方法、抽象工厂)
    设计模式:单例模式介绍及8种写法(饿汉式、懒汉式、Double-Check、静态内部类、枚举)
    设计模式七大原则及代码示例
  • 原文地址:https://www.cnblogs.com/sift/p/3597376.html
Copyright © 2020-2023  润新知