C # basic serialization (2)

Source: Internet
Author: User

I haven't written a blog for a long time. I 've been doing this for the last month. I don't have time to write a blog... today I'll talk about it again. The serialization we talked about last time is to serialize an object into an XML file. Today, let's talk about another way: serialize an object into a binary file. If there are too many, you can simply read the code.

[Serializable]
class Person
{

private string name;
private string age;
private string sex;

public string Name
{
set { name = value; }
get { return name; }
}
public string Age
{
get { return age; }
set { age = value; }
}

public string Sex
{
set { sex = value; }
get { return sex; }
}

}

This is a person class, which has three attributes: age, name, and gender. We added a [Serializable] feature to this class, which indicates that this class can be serialized. What if we don't want to serialize a certain attribute? Simply add the [NonSerialized] feature to that attribute. However, reading this code does not mean anything. Let's look at it.

Static void Main (string [] args)
{
Person p = new Person ();
P. Name = "xyl ";
P. Sex = "male ";
P. Age = "23 ";
BinaryFormatter Bf = new BinaryFormatter ();
Using (FileStream fs = new FileStream ("Person. dat", FileMode. Create, FileAccess. Write, FileShare. None ))
{
Bf. Serialize (fs, p );
}

Console. WriteLine ("serialization successful ");

Using (FileStream fsr = File. OpenRead ("Person. dat "))
{
Person p1 = (Person) Bf. Deserialize (fsr );
Console. WriteLine ("name = {0}, sex = {1}; age = {2}", p1.Name, p1.Sex, p1.Age );
}

}

This code is the code that serializes an object into a binary file and then deserializes it. First, we need to know the advantages of serializing an object into a binary file. Here we use the BinaryFormatter object. It not only persists the attribute field data in the object, but also persists the full qualified name of each type and the complete name of the Assembly. This data is ideal for transferring objects across. net application boundaries. Let's take a look at this code.

First, we instantiated a person object and assigned a value to the property. We created an instance of the BinaryFormatter object and then created a stream object. The parameters are path in sequence and the Creation Mode, how to access the file and how the process is shared. Then, use the Serialize () method to persist the object. In this case, the Person. dat file will be generated in the bin folder. Open it with.

Then use Deserialize for deserialization. The result is as follows:

This is the serialization process of a simple binary file with limited time. Today we will talk about it. However, when trying some demos, do not forget to reference the System. runtime. serialization. formatters. binary and System. IO. If something is wrong here, you are welcome to correct it ...... Good night

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.