Asp. NET interactive bitmap Form design (6)

Source: Internet
Author: User
Tags serialization versions
Asp.net| Interaction | Designed to make our objects serializable
To use a ASP.net object class in the application, we need to make another change to it. These classes need to be serializable to be able to pass data between the primary Web page and the Web page where the image is generated (detailed later). Serialization is the process of writing data from a class to a storage medium in a way that stores and/or passes data and deserializes later. Deserialization is the process of recreating an object from serialized data. We'll discuss this in depth in a future column.

Dr. GUI When you first write this application as a Windows forms application, you use only the common brushes and pens available in the. NET Framework and operating system preconfigured brushes and Pens classes. Because these are already allocated, keeping a reference to them is not an impediment, and you do not need to Dispose of them.

However, because pens and brushes are very complex objects and cannot be serializable, the Dr. GUI must change its policy, decide instead to store pen and brush colors, and then dynamically create pens and brushes when you need to draw and populate objects.

How do you make it serializable?
Serialization is an important part of the. NET Framework, making it easy to serialize objects as well.

We simply use the Serializable attribute to mark a class so that it can be serialized. (This is the same property as the property we used to mark it as a set of flags on the enumeration.) The syntax in C # and Visual Basic. NET is as follows:


C#

[Serializable]
Class Foo//...
Visual Basic

. NET _
Class Foo ' ...


Note: In addition to marking a class as serializable, you must also serialize all the data contained in the class, or the serialization framework throws an exception when you attempt to serialize the data.

To make a container serializable
A great advantage of the. NET Framework is that you can make the container class serializable. This means that if an object is stored in a serializable container, the container can automatically serialize the object.

So in this case, the DShapeList class contains two ArrayList objects. Because ArrayList is serializable, to make the dshapelist serializable, simply mark it as a Serializable property, as follows:


Visual Basic

. NET _
Public Class DShapeList
Dim wholelist as New ArrayList ()
Dim filledlist as New ArrayList ()
' ...


C#

[Serializable]
public class DShapeList {
ArrayList wholelist = new ArrayList ();
ArrayList filledlist = new ArrayList ();


Assuming that the objects we put in the dshapelist are serializable, you can serialize and deserialize the entire list using a single statement!

By the way, this is also a good change for the Windows forms version of the application because it allows us to write the drawing to a disk file and reload it.

You can draw three versions of an object, and any one can be used in any context
As you may have noticed, we have three versions of the draft object code: Each has a version that does not use the helper method we wrote above in C # and Visual Basic. NET, and the other is the version of the helper method that is used in Visual basic. Net.

There is a slight difference here: Data classes in Files using helper are marked as serializable, and data classes in other files are not marked as serializable.

However, note the following important point: if we go back and mark the data classes in all the files as serializable, then we will be able to use any class in any application. We will be able to mix with C # and Visual Basic. NET. And be able to use the code originally written for Windows forms applications in the ASP.net application.

This simple reuse of code means that the code you write is more valuable because the code can be reused in many different environments.


Related Article

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.