DataRow problem of serialization

Source: Internet
Author: User
Tags constructor implement serialization
The problem is that in. NET, objects of type DataRow do not support serialization, so what if you have DataRow type fields in an object that needs to be serialized? Oh, luckily the DataTable supports serialization. Therefore, we can customize the behavior of serialization and use a DataTable to wrap and DataRow the packets when serializing and deserializing.
In order to customize the serialization behavior, you must implement the ISerializable interface. Implement this interface to implement the GetObjectData method and the special constructor that is used when deserializing the object. The role of the former is to add the data encapsulated by the object to a container provided by the system, and the system will serialize the data, which is to take the deserialized data out of the container and then explicitly assign it to one of the fields of the object.
As the following example shows, the code that should be noted is marked in bold.

Using System;
Using System.Data;
Using System.Runtime.Serialization.Formatters.Binary;
Using System.Runtime.Serialization;
Using System.IO;
Using System.Security.Permissions;

Namespace Phenix. Dl
{
<summary>
A summary description of the Field.
</summary>
[Serializable]
public class Field:iserializable
{
private string Name= "";
Private DataRow Dr=null;
private string Title= "";
private int index=-1;

public int Index
{
Get{return This.index;}
Set{this.index=value;}
}

public string Title
{
Get{return This.title;}
Set{this.title=value;}
}

public string FieldName
{
Get{return THIS.name;}
Set{this.name=value;}
}

Public DataRow FieldInfo
{
Get{return this.dr;}
Set{this.dr=value;}
}

Public Field ()
{
//
TODO: Add constructor logic here
//
}

Protected Field (SerializationInfo info, StreamingContext context)//special constructor, automatically invoked when deserializing
{
This.name=info. GetString ("FieldName");
This.title=info. GetString ("Fieldtitle");
This.index=info. GetInt32 ("Fieldindex");
DataTable Dt=info. GetValue ("FieldInfo", New DataTable (). GetType ()) as DataTable;
This.dr=dt. Rows[0];
}

[SecurityPermissionAttribute (Securityaction.demand,serializationformatter=true)]
Called automatically when the public virtual void GetObjectData (SerializationInfo info, StreamingContext context)//is serialized
{
Info. AddValue ("FieldName", this.name);
Info. AddValue ("Fieldtitle", this.title);
Info. AddValue ("Fieldindex", This.index);
DataTable Dt=this.dr.table.clone (); DataRow cannot be added to two DataTable at the same time, you must first clone a
DataRow Row=dt. NewRow ();
Row. Itemarray=dr. ItemArray;

Dt. Rows.Add (row);
Info. AddValue ("FieldInfo", Dt,dt. GetType ());
}



public override string ToString ()
{
return this.name;
}

}
}



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.