Make our objects serializable
In order to use the printable object class in ASP. NET, we need to make another change to it. These classes need to be serializable so that data can be transferred between the main Web page and the Web page that generates the image (which will be detailed later ). Serialization is the process of writing data of a class to the storage medium in some way, so as to store and/or transmit data and deserialize it later. Deserialization is the process of re-creating objects from serialized data. We will discuss this issue in future columns in depth.
When writing this application as a Windows Forms Application, Dr. GUI only uses common paint Brushes and Pens available in the. NET Framework and pre-allocated Brushes and Pens classes in the operating system. Because these resources have been allocated, there is no obstacle to their reference and no need to Dispose them.
However, because pens and brushes are very complex objects and cannot be serialized, Dr. the GUI must change its policy, instead determine the color of the pen and paint brush, and then dynamically create a pen and paint brush when you need to draw and fill the object.
How to Make It serializable?
Serialization is an important part of the. NET Framework, so it also makes the serialization object work very simple.
We only need to mark a class with the Serializable attribute to make it Serializable. (This is the same as the attribute we used to mark it as a set of flag on 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 the class as serializable, all data contained in the class must be serializable. Otherwise, the serialization framework will throw an exception when attempting to serialize data.
Serializable containers
A major advantage of. NET Framework is that the container class can be serialized. This means that if the object is stored in a serializable container, the container can automatically serialize the object.
Therefore, in this example, the DShapeList class contains two ArrayList objects. Because ArrayList is Serializable, to make DShapeList Serializable, you only need to mark it as the Serializable attribute, as shown below:
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 ();
Suppose that the objects we put in DShapeList are all serializable, then we can use a single statement to serialize and deserialize the entire list!
By the way, this is also a good change to the Windows form version of the application, because it enables us to write the drawing into a disk file and reload it.
Three versions of the object that can be drawn; Any version can be used in any context
You may have noticed that we have three versions of printable object code: in C # and Visual Basic. NET each has a version that does not use the helper method we wrote above, and the other is Visual Basic.. NET.
There is a slight difference: the data classes in the files using helper are marked as serializable; the data classes in other files are not marked as serializable.
However, note the following important points: If we return and mark the data classes in all files as serializable, we will be able to use any class in any application. We will be able to mix C # and Visual Basic. NET. In addition, it can use the code originally written for Windows Forms applications in ASP. NET applications.
This simple code reuse means that the code you write is more valuable, because the code can be reused in many different environments.