首页 新闻 会员 周边 捐助

父类已实现ISerializable接口,子类没有添加可序列化特性如何序列化

0
悬赏园豆:10 [已解决问题] 解决于 2015-08-30 13:14
请问,在父类已经实现了ISerializable接口的情况下,子类Dog不添加[Serializable] 特性,如何实现序列化,代码在序列化的时候报错了,求回答……
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Runtime.Serialization;
 6 using System.Runtime.Serialization.Formatters.Binary;
 7 using System.Text;
 8 namespace DeSerializable
 9 {
10     public class Dog : Exception
11     {
12         public readonly string Name = "David Lee";
13         public static Dog Instrance = new Dog();
14         private Dog()
15         {
16         }
17         public override string ToString()
18         {
19             return string.Format("{0}小狗汪汪叫!", Name);
20         }
21     }
22     class Program
23     {
24         static void Main(string[] args)
25         {
26             Dog dog = DeepCopyDog();
27             Dog originalDog = Dog.Instrance;
28             if (ReferenceEquals(dog,originalDog))
29             {
30                 Console.WriteLine("深拷贝失败");
31             }
32             else
33             {
34                 Console.WriteLine("拷贝成功,恭喜");
35             }
36             Console.ReadLine();
37         }
38         public static Dog DeepCopyDog()
39         {
40             Dog dog = Dog.Instrance;
41             using (MemoryStream ms = new MemoryStream())
42             {
43                 BinaryFormatter bfFormatter = new BinaryFormatter();
44                 bfFormatter.Serialize(ms, dog);
45                 ms.Position = ms.Seek(0, SeekOrigin.Begin);
46                 return bfFormatter.Deserialize(ms) as Dog;
47             }
48         }
49     }
50 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Runtime.Serialization;
 6 using System.Runtime.Serialization.Formatters.Binary;
 7 using System.Text;
 8 namespace DeSerializable
 9 {
10     public class Dog : Exception
11     {
12         public readonly string Name = "David Lee";
13         public static Dog Instrance = new Dog();
14         private Dog()
15         {
16         }
17         public override string ToString()
18         {
19             return string.Format("{0}小狗汪汪叫!", Name);
20         }
21     }
22     class Program
23     {
24         static void Main(string[] args)
25         {
26             Dog dog = DeepCopyDog();
27             Dog originalDog = Dog.Instrance;
28             if (ReferenceEquals(dog,originalDog))
29             {
30                 Console.WriteLine("深拷贝失败");
31             }
32             else
33             {
34                 Console.WriteLine("拷贝成功,恭喜");
35             }
36             Console.ReadLine();
37         }
38         public static Dog DeepCopyDog()
39         {
40             Dog dog = Dog.Instrance;
41             using (MemoryStream ms = new MemoryStream())
42             {
43                 BinaryFormatter bfFormatter = new BinaryFormatter();
44                 bfFormatter.Serialize(ms, dog);
45                 ms.Position = ms.Seek(0, SeekOrigin.Begin);
46                 return bfFormatter.Deserialize(ms) as Dog;
47             }
48         }
49     }
50 }
View Code
 

 

 
小灯笼的主页 小灯笼 | 初学一级 | 园豆:193
提问于:2015-08-26 22:04
< >
分享
最佳答案
0
 [Serializable]
    public class Dog : Exception
    {
        public readonly string Name = "David Lee";
        public static Dog Instrance = new Dog();

        private Dog()
        {
        }

        protected Dog(SerializationInfo information, StreamingContext context)
        {
        }

        public override string ToString()
        {
            return string.Format("{0}小狗汪汪叫!", Name);
        }
    }

Serializable特性还是要添加的,另外还需要加上一个构造,才能保证序列化成功。

收获园豆:10
幻天芒 | 高人七级 |园豆:37205 | 2015-08-28 09:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册