110,536
社区成员
发帖
与我相关
我的任务
分享
[Serializable]
public class SerDbObj
{
public string ColumnName;
public object Value;
public SerDbObj(string cName, object value)
{
ColumnName = cName;
Value = value;
}
}
public class UBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
return ass.GetType(typeName);
}
}
/// <summary>
/// 用于序列化和反序列化对象
/// </summary>
public static class Serializable
{
/// <summary>
/// 将DBObject对象序列化成2进制数组
/// </summary>
/// <param name="a">要序列化的DBObject</param>
/// <returns>序列化成功以后返回数组,失败返回null</returns>
public static byte[] SerializabeDBobj(DBObjectBase a)
{
MemoryStream ms = new MemoryStream();
if (a == null) return null;
try
{
List<SerDbObj> abcd = new List<SerDbObj>();
foreach (DBObjectMeta x in a.ValuedColumns())
{
SerDbObj newB = new SerDbObj(x.Name, x.Value);
abcd.Add(newB);
}
BinaryFormatter fb = new BinaryFormatter();
byte[] myData;
fb.Serialize(ms, abcd);
myData = ms.ToArray();
return myData;
}
catch
{
return null;
}
}
/// <summary>
/// 使用2进制化后的数据反序列化获取DBObject对象
/// </summary>
/// <param name="bs">要反序列化的2进制数组</param>
/// <param name="c">要反序列化的类型</param>
/// <returns>如果失败将返回null</returns>
public static DBObjectBase DeSerializabeDBobj(byte[] bs, string tabName)
{
try
{
MemoryStream ms = new MemoryStream(bs);
BinaryFormatter fb = new BinaryFormatter();
fb.Binder = new UBinder();
List<SerDbObj> newTab = (List<SerDbObj>)fb.Deserialize(ms);
ms.Close();
DBObjectBase a = UnitedTransitResolution.DAL.DBObjectFactory.Create(tabName);
foreach (SerDbObj x in newTab)
{
a[x.ColumnName, 1].Value = x.Value;
}
return a;
}
catch
{
return null;
}
finally
{
}
}