前不久使用Webservice,传递参数为Hashtable,或是Arraylist中装载着Hashtable.
着实因为HT不能序列化难住了一下.现将序列化的方法备份.
using
System;using
System.IO;using
System.Collections;using
System.Xml.Serialization;using
System.Runtime.Serialization.Formatters.Binary;namespace
{
My.Utilities///{
{
}
public class Serializepublic Serialize()///{
XmlSerializer x =
MemoryStream m =
public static byte[] SerializeArrayList(ArrayList arr)null;null;byte[] brr;try{
x =
m =
x.Serialize(m,arr);
brr = m.ToArray();
}
{
}
new XmlSerializer(typeof(ArrayList));new MemoryStream();return brr;catch (Exception ex)throw ex;finally{
m.Close();
}
}
if (m != null)///{
XmlSerializer x =
MemoryStream m =
ArrayList arr;
public static ArrayList DeSerializeArrayList(byte[] brr)null;null;try{
x =
m =
arr = (ArrayList)x.Deserialize(m);
}
{
}
new XmlSerializer(typeof(ArrayList));new MemoryStream(brr);return arr;catch (Exception ex)throw ex;finally{
{
m.Close();
}
}
}
if (m != null)///{
BinaryFormatter b =
MemoryStream m =
public static byte[] SerializeHashtable(Hashtable ht)null;null;byte[] brr;try{
b =
m =
b.Serialize(m,ht);
brr = m.ToArray();
}
{
}
new BinaryFormatter();new MemoryStream();return brr;catch (Exception ex)throw ex;finally{
m.Close();
}
}
if(m != null)///{
BinaryFormatter b =
MemoryStream m =
Hashtable ht;
public static Hashtable DeSerializeHashtable(byte[] brr)null;null;try{
b =
m =
ht = (Hashtable)b.Deserialize(m);
}
{
}
new BinaryFormatter();new MemoryStream(brr);return ht;catch (Exception ex)throw ex;finally{
m.Close();
}
}
if (m != null)///{
ArrayList arr1 =
{
Hashtable ht = (Hashtable)arr[i];
arr1.Add(SerializeHashtable(ht));
}
brr = SerializeArrayList(arr1);
}
public static byte[] SerializeArrayListWithHashTabls(ArrayList arr)byte[] brr;new ArrayList();for(int i = 0; i < arr.Count; i++)return brr;///{
ArrayList arr =
ArrayList arr2 =
arr = DeSerializeArrayList(brr);
{
arr2.Add(DeSerializeHashtable((
}
}
}
}
public static ArrayList DeSerializearrayListWithhashTables(byte[] brr)null;new ArrayList();for(int i = 0; i < arr.Count; i++)byte[])arr[i]));return arr2;来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12476590/viewspace-134139/ ,如需转载,请注明出处,否则将追究法律责任。