以下文章提供了 C# 序列化的概述。将对象实例转换为数据流的过程称为序列化,并且对象实例的状态转换为数据流,因为它可以跨不同网络传输并持久保存在存储位置中。这就是序列化的优点,可以将转换后的数据流以跨平台兼容的格式跨不同网络传输,并将转换后的流数据以持久或非持久对象状态保存到存储介质中,以便可以在不同的网络上传输相同的副本。后来创建的。
以下是C#序列化对象的步骤:
演示 [ Serialized ] 类的示例类:
代码:
[Serializable] public class Check { public int code; public string name; }
考虑下面的示例类来演示 [ NonSerialized() ] 属性:
代码:
[Serializable] public class Check { public int code; public string name; [NonSerialized()] Public double price; }
下面给出了 C# 支持的序列化类型:
考虑下面的代码来演示 XmlAttribute 的使用:
代码:
[XmlAttribute("Name")] public string Name { get { return Name; } set { Name = val; } }
考虑下面的代码来演示 XmlSerializer 的使用:
代码:
XmlSerializer Serializer = new XmlSerializer(typeof(Prod)); using (TextWriter Writer = new StreamWriter(@"C:\Prod.xml")) { xmlSerializer.Serialize(Writer, prodObject); }
考虑下面的代码,通过实现 ISerialized 接口来演示自定义序列化:
代码:
[Serializable] public class Prod : ISerializable { public void GetObjectData(SerializationInfo information, StreamingContext cont) { //Usual code } }
下面给出的是 C# 序列化的示例:
演示序列化概念的 C# 程序。
代码:
using System; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; //a namespace called demo is created namespace Demo { //Serializable attribute is declared [Serializable] //a class check is defined which will be used for serialization class Check { public int identity; public String nam; static void Main(string[] args) { //an object of the check class is created to serialize it to the file Example.txt Check ob = new Check(); ob.identity = 10; ob.nam = "Shobha"; //a file stream is created IFormatter format = new BinaryFormatter(); Stream stream1 = new FileStream(@"E:\Example.txt",FileMode.Create,FileAccess.Write); //serialization of the object of the class check is done format.Serialize(stream1, ob); stream1.Close(); //a file stream is created stream1 = new FileStream(@"E:\Example.txt",FileMode.Open,FileAccess.Read); //the object of the class check is deserialized Check ob1 = (Check)format.Deserialize(stream1); //the data is written to the console Console.WriteLine(ob1.identity); Console.WriteLine(ob1.nam); Console.ReadKey(); } } }
Output:
In the above program, a namespace called demo is defined. Then a Serializable attribute is defined. A class check is defined to demonstrate the concept of serialization using this class. Two properties identity and nam are defined in the class to which the values 10 and Shobha are assigned respectively. Then an object of the check class is created to serialize it to the file Example.txt. Then a formatter class is defined to convert the object of the class check to a binary stream.
Then a file stream object is created to open the file Example.txt in write mode to write the values of the properties identity and nam into it. Then serialize method is used to transfer the binary data into the text file. Finally, We use deserialize method to deserialize the contents of the text file Example.txt and the data is written to the console as shown in the output snapshot above.
以上是C# 序列化的详细内容。更多信息请关注PHP中文网其他相关文章!