zoukankan      html  css  js  c++  java
  • .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
            }
        }

  • 相关阅读:
    【BZOJ】1626: [Usaco2007 Dec]Building Roads 修建道路(kruskal)
    【BZOJ】1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛(lis)
    【BZOJ】1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏(刷水严重)
    【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)
    【BZOJ】1625: [Usaco2007 Dec]宝石手镯(01背包)
    【BZOJ】1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
    【BZOJ】1618: [Usaco2008 Nov]Buying Hay 购买干草(dp)
    【BZOJ】1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛(dp/-bfs)
    【BZOJ】1613: [Usaco2007 Jan]Running贝茜的晨练计划(dp)
    【BZOJ】1612: [Usaco2008 Jan]Cow Contest奶牛的比赛(floyd/dfs)
  • 原文地址:https://www.cnblogs.com/sift/p/3597376.html
Copyright © 2011-2022 走看看