- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
How to Create Classes That Can Be Serialized in VB.NET
How to Create Classes That Can Be Serialized Encode QR Code 2d Barcode In Visual Basic .NET Using Barcode printer for .NET Control to generate, create QR image in Visual Studio .NET applications. www.OnBarcode.comReading QR-Code In Visual Basic .NET Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comYou can make custom classes serializable and deserializable by adding the Serializable attribute to the class. This is important because it allows you, or other developers using your class, to store or transfer instances of the class easily. Even if you do not immediately need serialization, it is good practice to enable it for future use. If you are satisfied with the default handling of the serialization, no other code besides the Serializable attribute is necessary. When your class is serialized, the runtime serializes all members, including private members. Bar Code Printer In Visual Basic .NET Using Barcode printer for VS .NET Control to generate, create barcode image in .NET framework applications. www.OnBarcode.comBarcode Decoder In Visual Basic .NET Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comNOTE
Generate Denso QR Bar Code In C# Using Barcode encoder for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET framework applications. www.OnBarcode.comEncode QR-Code In VS .NET Using Barcode creation for ASP.NET Control to generate, create QR image in ASP.NET applications. www.OnBarcode.comSecurity Concerns with Serialization
Generate QR Code 2d Barcode In VS .NET Using Barcode creation for .NET framework Control to generate, create QR Code ISO/IEC18004 image in .NET applications. www.OnBarcode.comPDF417 Generation In VB.NET Using Barcode generator for VS .NET Control to generate, create PDF417 image in VS .NET applications. www.OnBarcode.comSerialization can allow other code to see or modify object instance data that would be inaccessible otherwise. Therefore, code performing serialization requires the SecurityPermission attribute (from the System.Security.Permissions namespace) with the SerializationFormatter flag specified. Under default policy, this permission is not given to Internet-downloaded or intranet code; only code on the local computer is granted this permission. The GetObjectData method should be explicitly protected either by demanding the SecurityPermission attribute with the SerializationFormatter flag specified, as illustrated in the sample code in Lesson 3, or by demanding other permissions that specifically help protect private data. For more information about code security, refer to 12, User and Data Security. Print Code 39 Extended In VB.NET Using Barcode drawer for .NET Control to generate, create Code 39 Full ASCII image in .NET framework applications. www.OnBarcode.comData Matrix 2d Barcode Generator In Visual Basic .NET Using Barcode drawer for VS .NET Control to generate, create ECC200 image in .NET applications. www.OnBarcode.com 5
Barcode Drawer In VB.NET Using Barcode maker for Visual Studio .NET Control to generate, create bar code image in .NET framework applications. www.OnBarcode.comEAN 8 Generator In VB.NET Using Barcode maker for .NET framework Control to generate, create EAN-8 Supplement 5 Add-On image in .NET applications. www.OnBarcode.comSerialization
Code 128 Code Set B Maker In .NET Framework Using Barcode creator for Reporting Service Control to generate, create Code 128 Code Set B image in Reporting Service applications. www.OnBarcode.comPDF 417 Creator In None Using Barcode generation for Excel Control to generate, create PDF 417 image in Excel applications. www.OnBarcode.comYou can control serialization of your classes to improve the efficiency of your class or to meet custom requirements. The following sections discuss how to customize how your class behaves during serialization. Paint Bar Code In VS .NET Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications. www.OnBarcode.comBarcode Reader In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comHow to Disable Serialization of Specific Members
Scanning Barcode In Visual Studio .NET Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comUCC - 12 Generation In None Using Barcode printer for Online Control to generate, create UCC - 12 image in Online applications. www.OnBarcode.comSome members of your class, such as temporary or calculated values, might not need to be stored. For example, consider the following class, ShoppingCartItem: Print Data Matrix In C#.NET Using Barcode maker for .NET framework Control to generate, create DataMatrix image in .NET framework applications. www.OnBarcode.comPrint Code 39 In Java Using Barcode encoder for Android Control to generate, create Code39 image in Android applications. www.OnBarcode.com' VB <Serializable()> Class ShoppingCartItem Public productId As Integer Public price As Decimal Public quantity As Integer Public total As Decimal Public Sub New(ByVal _productID As Integer, ByVal _price As Decimal, _ ByVal _quantity As Integer) MyBase.New productId = _productID price = _price quantity = _quantity total = price * quantity End Sub End Class // C# [Serializable] class ShoppingCartItem { public int productId; public decimal price; public int quantity; public decimal total; public ShoppingCartItem(int _productID, decimal _price, int _quantity) { productId = _productID; price = _price; quantity = _quantity; total = price * quantity; } } The ShoppingCartItem includes three members that must be provided by the application when the object is created. The fourth member, total, is dynamically calculated by multiplying the price and quantity. If this class were serialized as-is, the total would be stored with the serialized object, wasting a small amount of storage. To reduce the size of the serialized object (and thus reduce storage requirements Lesson 1: Serializing Objects
when writing the serialized object to a disk and bandwidth requirements when transmitting the serialized object across the network), add the NonSerialized attribute to the total member as follows: ' VB <NonSerialized()> Public total As Decimal // C# [NonSerialized] public decimal total; Now, when the object is serialized, the total member is omitted. Similarly, the total member is not initialized when the object is deserialized. However, the value for total must still be calculated before the deserialized object is used. To enable your class to initialize a nonserialized member automatically, use the IDeserializationCallback interface and then implement IDeserializationCallback.OnDeserialization. Each time your class is deserialized, the runtime calls the IDeserializationCallback .OnDeserialization method after deserialization is complete. The following example shows the ShoppingCartItem class modified to not serialize the total value and to automatically calculate the value upon deserialization. The changes are shown in bold: ' VB <Serializable()> Class ShoppingCartItem Implements IDeserializationCallback Public productId As Integer Public price As Decimal Public quantity As Integer <NonSerialized()> Public total As Decimal Public Sub New(ByVal _productID As Integer, ByVal _price As Decimal, _ ByVal _quantity As Integer) MyBase.New productId = _productID price = _price quantity = _quantity total = price * quantity End Sub Sub IDeserializationCallback_OnDeserialization(ByVal sender As Object) _ Implements IDeserializationCallback.OnDeserialization ' After deserialization, calculate the total total = price * quantity End Sub End Class
|
|