C# 序列化与反序列化

简介:

C# 序列化与反序列化

using  System.IO;
   using  System.Runtime.Serialization.Formatters.Binary;
   using  System.Runtime.Serialization.Formatters.Soap;
   using  System.Xml.Serialization;
   using  System.Runtime.Serialization;
   public  class  SerializeAndDeserialize
   {
       //1、使用BinaryFormatter进行串行化
       public  void  SerializeNow()
       {
           ClassToSerialize c = new  ClassToSerialize();
           FileStream fileStream = new  FileStream( "c:\\temp.dat" , FileMode.Create);
           BinaryFormatter b = new  BinaryFormatter();
           b.Serialize(fileStream, c);
           fileStream.Close();
       }
 
       public  void  DeSerializeNow()
       {
           ClassToSerialize c = new  ClassToSerialize();
           c.Sex = "kkkk" ;
           FileStream fileStream = new  FileStream( "c:\\temp.dat" , FileMode.Open, FileAccess.Read, FileShare.Read);
             BinaryFormatter b = new  BinaryFormatter();
           c = b.Deserialize(fileStream) as  ClassToSerialize;
           Console.WriteLine( "Output :"  + c.name);
           Console.WriteLine( "Output :"  + c.id);
           Console.WriteLine( "Output :"  + c.Sex);
           fileStream.Close();
       }
 
       // 2、使用SoapFormatter进行串行化
       public  void  SerializeNow_Soap()
       {
           ClassToSerialize c = new  ClassToSerialize();
           FileStream fileStream = new  FileStream( "c:\\temp2.dat" , FileMode.Create);
           SoapFormatter s = new  SoapFormatter();
           s.Serialize(fileStream, c);
           fileStream.Close();
       }
 
       public  void  DeSerializeNow_Soap()
       {
           ClassToSerialize c = new  ClassToSerialize();
           c.Sex = "kkkk" ;
           FileStream fileStream = new  FileStream( "c:\\temp2.dat" , FileMode.Open, FileAccess.Read, FileShare.Read);
           SoapFormatter s = new  SoapFormatter();
           c = s.Deserialize(fileStream) as  ClassToSerialize;
           Console.WriteLine( "Output :"  + c.name);
           Console.WriteLine( "Output :"  + c.id);
           Console.WriteLine( "Output :"  + c.Sex);
           fileStream.Close();
       }
 
       //3、使用XmlSerializer进行串行化
       public  void  XMLSerialize()
       {
           Person c = new  Person( "cyj" );
           c.Courses = new  Course[2];
           c.Courses[0] = new  Course( "英语" , "交流工具" );
           c.Courses[1] = new  Course( "数学" , "自然科学" );
           XmlSerializer xs = new  XmlSerializer( typeof (Person));
           Stream stream = new  FileStream( "c:\\cyj.HTML" , FileMode.Create, FileAccess.Write, FileShare.Read);
           xs.Serialize(stream, c);
           stream.Close();
       }
       public  void  XMLDeserialize()
       {
           XmlSerializer xs = new  XmlSerializer( typeof (Person));
           Stream stream = new  FileStream( "C:\\cyj.XML" , FileMode.Open, FileAccess.Read, FileShare.Read);
           Person p = xs.Deserialize(stream) as  Person;
           Console.WriteLine( "Output :"  + p.Name);
           Console.WriteLine( "Output :"  + p.Age.ToString());
           Console.WriteLine( "Output :"  + p.Courses[0].Name);
           Console.WriteLine( "Output :"  + p.Courses[0].Description);
           Console.WriteLine( "Output :"  + p.Courses[1].Name);
           Console.WriteLine( "Output :"  + p.Courses[1].Description);
           stream.Close();
       }
 
       //4、自定义序列化
       public  void  OtherEmployeeClassTest()
       {
           Employee mp = new  Employee();
           mp.EmpId = 10;
           mp.EmpName = "邱枫" ;
           mp.NoSerialString = "你好呀" ;
           Stream steam = File.Open( "c:\\temp3.dat" , FileMode.Create);
           BinaryFormatter bf = new  BinaryFormatter();
           Console.WriteLine( "Output :"  + "Writing Employee Info:" );
           bf.Serialize(steam, mp);
           steam.Close();
           mp = null ;
           //反序列化
           Stream steam2 = File.Open( "c:\\temp3.dat" , FileMode.Open);
           BinaryFormatter bf2 = new  BinaryFormatter();
           Console.WriteLine( "Output :"  + "Reading Employee Info:" );
           Employee mp2 = (Employee)bf2.Deserialize(steam2);
           steam2.Close();
           Console.WriteLine( "Output :"  + mp2.EmpId);
           Console.WriteLine( "Output :"  + mp2.EmpName);
           Console.WriteLine( "Output :"  + mp2.NoSerialString);
       }
   }
 
   [Serializable]
   public  class  ClassToSerialize
   {
       public  int  id = 100;
       public  string  name = "Name" ;
       [NonSerialized]
       public  string  Sex = "男" ;
   }
 
 
   [Serializable]
   public  class  Person
   {
       private  string  name;
       public  string  Name
       {
           get
           {
               return  name;
           }
           set
           {
               name = value;
           }
       }
 
 
       public  string  Sex;
       public  int  Age = 31;
       public  Course[] Courses;
 
       public  Person()
       {
       }
       public  Person( string  Name)
       {
           name = Name;
           Sex = "男" ;
       }
   }
   [Serializable]
   public  class  Course
   {
       public  string  Name;
       [XmlIgnore]
       public  string  Description;
       public  Course()
       {
       }
       public  Course( string  name, string  description)
       {
           Name = name;
           Description = description;
       }
   }
 
 
   [Serializable]
   public  class  Employee : ISerializable
   {
       public  int  EmpId = 100;
       public  string  EmpName = "work hard work smart work happy" ;
       [NonSerialized]
       public  string  NoSerialString = "NoSerialString-Test" ;
       public  Employee()
       {
       }
       private  Employee(SerializationInfo info, StreamingContext ctxt)
       {
           EmpId = ( int )info.GetValue( "EmployeeId" , typeof ( int ));
           EmpName = (String)info.GetValue( "EmployeeName" , typeof ( string ));
       }
       public  void  GetObjectData(SerializationInfo info, StreamingContext ctxt)
       {
           info.AddValue( "EmployeeId" , EmpId);
           info.AddValue( "EmployeeName" , EmpName);
       }
   }

 

 

C# 序列号与反序列化总结

 1、 C# 序列化与反序列化(此文)

2、C# Serializable对象序列化的作用 



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/12/22/2297921.html,如需转载请自行联系原作者

目录
相关文章
|
存储 Java
【IO面试题 四】、介绍一下Java的序列化与反序列化
Java的序列化与反序列化允许对象通过实现Serializable接口转换成字节序列并存储或传输,之后可以通过ObjectInputStream和ObjectOutputStream的方法将这些字节序列恢复成对象。
|
4月前
|
存储 安全 IDE
说一说序列化与反序列化中存在的问题
本文详细解析了Java中的序列化机制,包括序列化的概念、实现方式及应用场景。通过Student类的实例演示了对象的序列化与反序列化过程,并分析了`Serializable`接口的作用以及`serialVersionUID`的重要意义。此外,文章还探讨了如何通过自定义`readObject()`方法增强序列化的安全性,以及解决可序列化单例模式中可能产生的多实例问题。最后提供了代码示例和运行结果,帮助读者深入理解序列化的原理与实践技巧。
114 2
|
4月前
|
JSON JavaScript 前端开发
Go语言JSON 序列化与反序列化 -《Go语言实战指南》
本文介绍了 Go 语言中使用 `encoding/json` 包实现 JSON 与数据结构之间的转换。内容涵盖序列化(`Marshal`)和反序列化(`Unmarshal`),包括基本示例、结构体字段标签的使用、控制字段行为的标签(如 `omitempty` 和 `-`)、处理 `map` 和切片、嵌套结构体序列化、反序列化未知结构(使用 `map[string]interface{}`)以及 JSON 数组的解析。最后通过表格总结了序列化与反序列化的方法及类型要求,帮助开发者快速掌握 JSON 数据处理技巧。
|
10月前
|
JSON 数据格式 索引
Python中序列化/反序列化JSON格式的数据
【11月更文挑战第4天】本文介绍了 Python 中使用 `json` 模块进行序列化和反序列化的操作。序列化是指将 Python 对象(如字典、列表)转换为 JSON 字符串,主要使用 `json.dumps` 方法。示例包括基本的字典和列表序列化,以及自定义类的序列化。反序列化则是将 JSON 字符串转换回 Python 对象,使用 `json.loads` 方法。文中还提供了具体的代码示例,展示了如何处理不同类型的 Python 对象。
314 1
|
10月前
|
存储 安全 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第22天】在Java的世界里,对象序列化和反序列化是数据持久化和网络传输的关键技术。本文将带你了解如何在Java中实现对象的序列化与反序列化,并探讨其背后的原理。通过实际代码示例,我们将一步步展示如何将复杂数据结构转换为字节流,以及如何将这些字节流还原为Java对象。文章还将讨论在使用序列化时应注意的安全性问题,以确保你的应用程序既高效又安全。
|
11月前
|
存储 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第9天】在Java的世界里,对象序列化是连接数据持久化与网络通信的桥梁。本文将深入探讨Java对象序列化的机制、实践方法及反序列化过程,通过代码示例揭示其背后的原理。从基础概念到高级应用,我们将一步步揭开序列化技术的神秘面纱,让读者能够掌握这一强大工具,以应对数据存储和传输的挑战。
|
11月前
|
存储 安全 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第3天】在Java编程的世界里,对象序列化与反序列化是实现数据持久化和网络传输的关键技术。本文将深入探讨Java序列化的原理、应用场景以及如何通过代码示例实现对象的序列化与反序列化过程。从基础概念到实践操作,我们将一步步揭示这一技术的魅力所在。
|
12月前
|
JSON fastjson Java
niubility!即使JavaBean没有默认无参构造器,fastjson也可以反序列化。- - - - 阿里Fastjson反序列化源码分析
本文详细分析了 Fastjson 反序列化对象的源码(版本 fastjson-1.2.60),揭示了即使 JavaBean 沲有默认无参构造器,Fastjson 仍能正常反序列化的技术内幕。文章通过案例展示了 Fastjson 在不同构造器情况下的行为,并深入探讨了 `ParserConfig#getDeserializer` 方法的核心逻辑。此外,还介绍了 ASM 字节码技术的应用及其在反序列化过程中的角色。
277 10
|
12月前
|
JSON 安全 编译器
扩展类实例的序列化和反序列化
扩展类实例的序列化和反序列化
120 1
|
12月前
|
存储 XML JSON
用示例说明序列化和反序列化
用示例说明序列化和反序列化
153 1

热门文章

最新文章