In many cases, we need to serialize objects into strings and save them to memory, disk, or page. viewstate. For various reasons, we hope that the serialization results can be as small as possible and as simple as possible. Even other methods (such as regular expressions) can parse the data. The result of binaryformatter is converted into a string (or base64) with too much length, while xmlserializer has limited support for data types. Obviously, the built-in serialization engine is not enough to meet our needs.
The followingCodeIt may not be complete yet and is for reference only. The content is simple and will not be detailed. /// <Summary>
/// Serialization
/// </Summary>
Public static string serializeobject (Object O)
{
Char sep1 = '| ';
Char sep2 = ',';
Char sep3 = ';
Stringbuilder sb = new stringbuilder ();
Fieldinfo [] fields = O. GetType (). getfields (bindingflags. instance | bindingflags. Public |
Bindingflags. nonpublic );
Foreach (fieldinfo field in fields)
{
Object value = field. getvalue (O );
If (value! = NULL)
{< br> If (field. fieldtype. getinterface ("idictionary ")! = NULL)
{< br> foreach (Object key in (value as idictionary ). keys)
{< br> Sb. appendformat ("{0} {3} {1} {2}", key, (value as idictionary) [Key], sep2, sep3 );
}
If (SB [SB. length-1] = sep2) Sb. remove (sb. length-1, 1);
}< br> else if (field. fieldtype. getinterface ("ilist ")! = NULL)
{< br> foreach (Object V in (value as ilist)
{< br> Sb. appendformat ("{0} {1}", V, sep2);
}
If (SB [SB. Length-1] = sep2) sb. Remove (sb. Length-1, 1 );
}
Else if (field. fieldtype = typeof (Boolean ))
{
SB. append (bool) value? "T ":"");
}
Else
{
SB. append (value );
}
}
SB. append (sep1 );
}
If (SB [SB. Length-1] = sep1) sb. Remove (sb. Length-1, 1 );
Return sb. tostring ();
}
/// <Summary>
/// Deserialization
/// </Summary>
Public static t deserializeobject <t> (string S)
Where T: New ()
{
Char sep1 = '| ';
Char sep2 = ',';
Char sep3 = ';
T o = new T ();
Fieldinfo [] fields = O. GetType (). getfields (bindingflags. instance | bindingflags. Public |
Bindingflags. nonpublic );
String [] values = S. Split (sep1 );
For (INT I = 0; I <fields. length; I ++)
{
Fieldinfo field = Fields [I];
If (string. isnullorempty (Values [I]) continue;
If (field. fieldtype. getinterface ("idictionary ")! = NULL)
{
String [] Vs = values [I]. Split (sep2 );
Idictionary dictionary = field. getvalue (o) As idictionary;
Type key = field. fieldtype. isgenerictype?
Field. fieldtype. getgenericarguments () [0]: typeof (object );
Type value = field. fieldtype. isgenerictype?
Field. fieldtype. getgenericarguments () [1]: typeof (object );
If (Dictionary = NULL)
{
Dictionary = (idictionary) activator. createinstance (field. fieldtype );
Field. setvalue (O, dictionary );
}
Foreach (string V in)
{
String [] NS = V. Split (sep3 );
Dictionary. Add (convert. changetype (NS [0], Key), convert. changetype (NS [1], value ));
}
}
Else if (field. fieldtype. getinterface ("ilist ")! = NULL)
{
String [] Vs = values [I]. Split (sep2 );
If (field. fieldtype. isarray)
{
Type T = field. fieldtype. getelementtype ();
Array array = array. createinstance (T, vs. Length );
For (INT x = 0; x <vs. length; X ++)
{
Array. setvalue (convert. changetype (vs [X], T), X );
}
Field. setvalue (O, array );
}
Else
{
Ilist list = field. getvalue (o) As ilist;
Type T = field. fieldtype. isgenerictype?
Field. fieldtype. getgenericarguments () [0]: typeof (object );
If (list = NULL)
{
List = (ilist) activator. createinstance (field. fieldtype );
Field. setvalue (O, list );
}
Foreach (string V in)
{
List. Add (convert. changetype (v, t ));
}
}
}
Else if (field. fieldtype = typeof (Boolean ))
{
Field. setvalue (O, values [I] = "T "? True: false );
}
Else if (field. fieldtype. isenum)
{
Field. setvalue (O, enum. parse (field. fieldtype, values [I], true ));
}
Else
{
Field. setvalue (O, convert. changetype (Values [I], field. fieldtype ));
}
}
Return O;
}
Test code
[Serializable]
Public class myclass
{
Private int valuetype;
Public int valuetype
{
Get {return valuetype ;}
Set {valuetype = value ;}
}
Private object OBJ;
Public object
{
Get {return OBJ ;}
Set {OBJ = value ;}
}
Private bool Boolean;
Public bool Boolean
{
Get {return Boolean ;}
Set {Boolean = value ;}
}
Private string [] array;
Public String [] Array
{
Get {return array ;}
Set {array = value ;}
}
Private list <string> list;
Public list <string> List
{
Get {return list ;}
Set {list = value ;}
}
Private arraylist;
Public arraylist
{
Get {return arraylist ;}
Set {arraylist = value ;}
}
Private hashtable;
Public hashtable
{
Get {return hashtable ;}
Set {hashtable = value ;}
}
Private dictionary <string, int> dictionary;
Public dictionary <string, int> dictionary
{
Get {return dictionary ;}
Set {dictionary = value ;}
}
}
Class Program
{
Static void main (string [] ARGs)
{
// Test ();
Myclass o = new myclass ();
O. List = new list <string> ();
O. dictionary = new dictionary <string, int> ();
O. arraylist = new arraylist ();
O. hashtable = new hashtable ();
O. valuetype = 123456;
O. Object = datetime. now;
O. boolean = true;
O. dictionary. Add ("dict1", 1 );
O. dictionary. Add ("dict2", 2 );
O. array = new string [] {"array1", "array2", "array3 "};
O. List. Add ("list1 ");
O. List. Add ("list2 ");
O. arraylist. Add ("arraylist1 ");
O. arraylist. Add ("arraylist2 ");
O. hashtable. Add ("hashtable1", 1 );
O. hashtable. Add ("hashtable2", 2 );
// Serializeobject
String S = serializeobject (O );
Console. writeline (s );
Myclass M = deserializeobject <myclass> (s );
Console. writeline (serializeobject (m ));
// Binaryformatter
Binaryformatter binary = new binaryformatter ();
Memorystream stream = new memorystream ();
Binary. serialize (stream, O );
S = convert. tobase64string (stream. toarray ());
Console. writeline (s );
}
}