How to serialize complex objects such as control

Source: Internet
Author: User

. NET Framework provides the ability to serialize and deserialize objects. With this mechanism, we can store the status of the object instance to the storage media, or pass the object from one place to another.

. NET Framework provides some classes for serialization. One is binnaryformatter, which serializes objects in binary format. The other is soapformatter, which uses the SOAP format (based on the XML format) to serialize objects. You can also use xmlserializer to serialize objects into XML format. However, this serialization mechanism has certain limitations. It can only serialize specific objects. These objects are either derived from marshalbyrefobject or simple type objects marked as serializable. Complex objects such as system. Windows. Forms. control cannot be supported.

Today, I want to introduce how to serialize complex objects such as system. Windows. Forms. Control. Before introducing it, let's take a look at the serialization and deserialization processes.

 

Separation of complex logic, the simplest logic is only two steps, the first step-serialization process: object data in memory is represented in XML format. Step 2-deserialization: construct a new object and assign the data in XML format to the new object. In this way, the original object and the newly constructed object will have the same data, indicating the same state.

IDE developers such as vs2005 and vs2008 are often used. Especially those who have worked in design time development may notice that this process is similar to the process in which the IDE automatically generates control code.

When using IDE, we drag a Textbox Control to form in the design environment. Ide creates a Textbox Control in the design environment, generate related code in C # Or VB format. This stage is called design time ). This process is similar to the serialization process, the first step we just mentioned. When we click "run", the code in the text format will be compiled and run again. A new Textbox Control is generated based on the compiled code and displayed on the form. This stage is called runtime ). This process is similar to the deserialization process in step 2. As you can see, IDE orders control instances into C #, VB, and other code in designtime. In runtime, code is compiled and run, and a new control instance is generated to ensure that the design status in designtime is consistent with that displayed in runtime. In fact, this is also a way of serialization and deserialization.

Since this process of IDE is also serialized and deserialized, how does ide implement the serialization and deserialization of complex controls? This may require another article to be clear. I will not study it here. But we can know that IDE is capable of doing this. We can also use this capability of IDE to serialize and deserialize complex controls.

The following code demonstrates how to use this ide capability to serialize an object.

Saveobject
1/** // <summary>
2 // saves the <see CREF = "T: object"/> to the given stream.
3 /// </Summary>
4 /// <Param name = "value">
5 // The <see CREF = "T: object"/> will be serialized to save.
6 /// </param>
7 // <Param name = "stream">
8 // the stream to which the <see CREF = "T: object"/> will be serialized.
9 /// </param>
10 public static void saveobject (object value, stream Stream)
11 {
12 if (Stream = NULL)
13 {
14 throw new argumentnullexception ();
15}
16
17 using (designsurface = new designsurface ())
18 {
19 designsurface. beginload (New defaultcodedomdesignerloader ());
20 componentserializationservice serializationservice = designsurface. getservice (typeof (componentserializationservice) as componentserializationservice;
21
22 serializationstore store = serializationservice. createstore ();
23 serializationservice. serialize (store, value );
24 store. Save (Stream );
25}
26}
27

The following code demonstrates how to use ide To deserialize an object.

Loadobject
1/** // <summary>
2 // loads a <see CREF = "T: object"/> from the given stream.
3 /// </Summary>
4 /// <Param name = "stream">
5 // The stream from which to load the <see CREF = "T: object"/>.
6 /// </param>
7 /// <returns>
8 // The loaded <see CREF = "T: object"/>.
9 /// </returns>
10 public static object loadobject (Stream)
11 {
12 if (Stream = NULL)
13 {
14 throw new argumentnullexception ();
15}
16
17 using (designsurface = new designsurface ())
18 {
19 designsurface. beginload (New defaultcodedomdesignerloader ());
20 componentserializationservice serializationservice = designsurface. getservice (typeof (componentserializationservice) as componentserializationservice;
21
22 serializationstore store = serializationservice. loadstore (Stream );
23 icollection collection = serializationservice. deserialize (store );
24
25 foreach (icomponent associatedcomponent in designsurface. componentcontainer. components)
26 {
27 designsurface. componentcontainer. Remove (associatedcomponent );
28}
29
30 ienumerator enumerator = collection. getenumerator ();
31 if (enumerator. movenext ())
32 {
33 return enumerator. Current;
34}
35
36 return NULL;
37}
38}

With this capability of IDE, we can also do many things, such as the deep clone control. Sometimes, we need to clone a complex control, but the method is limited. either use the reflection enumeration control to assign values to all fields or assign values to all attributes one by one. The dependency between attributes and how to handle referenced attributes are all difficult issues. Control serialization and deserialization can be used to easily clone controls. For example, you can get the C #, VB, and other code of the control in runtime, because the intermediate product of serialization is C #, VB and other code, so this mechanism is easy to implement. I have written a class library for the code and can contact me if necessary. There are too many codes, so I will not post them here.

Supplement: Now I have uploaded the source code. You can download it here.

Original article. For more information, see the source. Kevinshan E-mail: txhak@163.com.

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.