|
楼主 |
发表于 2016-10-26 17:51:14
|
显示全部楼层
//序列化
public static bool serialObjToStr(object obj, out string serializedStr)
{
bool serializeOK = false;
serializedStr = "";
try
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, obj);
serializedStr = System.Convert.ToBase64String(memoryStream.ToArray());
serializeOK = true;
}
catch
{
serializeOK = false;
}
return serializeOK;
}
//反序列化
public static bool deserializeStrToObj(string serializedStr, out object deserializedObj)
{
bool deserializedOK = false;
deserializedObj = null;
try
{
byte[] restoredBytes = System.Convert.FromBase64String(serializedStr);
MemoryStream restoredMemoryStream = new MemoryStream(restoredBytes);
BinaryFormatter binaryFormatter = new BinaryFormatter();
deserializedObj = binaryFormatter.Deserialize(restoredMemoryStream);
deserializedOK = true;
}
catch
{
deserializedOK = false;
}
return deserializedOK;
}
然后将list<struct> 序列化就可以存储在数据库中了;需要再处理的时候,从数据库读出来通过装箱拆箱就可以将string转换为list<struct>了。 |
|