[Serializable]
public class TestObj : Dictionary<string, object>
{
//.....
}public class CommonUtil
{
public static byte[] Serial(object obj)
{
try
{
BinaryFormatter formater = new BinaryFormatter();
MemoryStream memory = new MemoryStream();
formater.Serialize(memory, obj);
return memory.GetBuffer();
}
catch (Exception er)
{
Helper.Logger.Error(er.ToString());
return null;
}
} public static object Deserial(byte[] bytes)
{
try
{
MemoryStream ms = new MemoryStream(bytes);
object result = Deserial(ms);
return result;
}
catch (Exception er)
{
Helper.Logger.Error(er.ToString());
return null;
}
} public static object Deserial(Stream s)
{
try
{
BinaryFormatter formater = new BinaryFormatter();
object result = formater.Deserialize(s);//异常:未找到反序列化“TestObj”类型对象的构造函数。
return result;
}
catch (Exception er)
{
Helper.Logger.Error(er.ToString());
return null;
}
}
}//测试代码: TestObjaso1 = new TestObj(); aso1["a"]=1; byte[] bytes = CommonUtil.Serial(aso1); TestObjaso2 = CommonUtil.Deserial(bytes) as TestObj;//CommonUtil.Deserial内有异常未找到反序列化“TestObj”类型对象的构造函数。这句话,需要反序列化对象类型的构造函数?:
formater.Deserialize(s);//异常:未找到反序列化“TestObj”类型对象的构造函数。那对构造函数的参数,有没有需求哪位大哥能说一下,谢谢。现在一边找资料,一边在这等答案了
public class TestObj : Dictionary<string, object>
{
//.....
}public class CommonUtil
{
public static byte[] Serial(object obj)
{
try
{
BinaryFormatter formater = new BinaryFormatter();
MemoryStream memory = new MemoryStream();
formater.Serialize(memory, obj);
return memory.GetBuffer();
}
catch (Exception er)
{
Helper.Logger.Error(er.ToString());
return null;
}
} public static object Deserial(byte[] bytes)
{
try
{
MemoryStream ms = new MemoryStream(bytes);
object result = Deserial(ms);
return result;
}
catch (Exception er)
{
Helper.Logger.Error(er.ToString());
return null;
}
} public static object Deserial(Stream s)
{
try
{
BinaryFormatter formater = new BinaryFormatter();
object result = formater.Deserialize(s);//异常:未找到反序列化“TestObj”类型对象的构造函数。
return result;
}
catch (Exception er)
{
Helper.Logger.Error(er.ToString());
return null;
}
}
}//测试代码: TestObjaso1 = new TestObj(); aso1["a"]=1; byte[] bytes = CommonUtil.Serial(aso1); TestObjaso2 = CommonUtil.Deserial(bytes) as TestObj;//CommonUtil.Deserial内有异常未找到反序列化“TestObj”类型对象的构造函数。这句话,需要反序列化对象类型的构造函数?:
formater.Deserialize(s);//异常:未找到反序列化“TestObj”类型对象的构造函数。那对构造函数的参数,有没有需求哪位大哥能说一下,谢谢。现在一边找资料,一边在这等答案了
解决方案 »
- winform怎么判断右键事件从哪个控件上发生的?
- 求救
- VS 2005 中MDI窗体显示的问题
- 初学者请教一个问题,谢谢,站内搜了,没找到答案
- 急,Owc11控件Y轴日期显示问题
- 谁会用vs2005 + vss2005?
- HttpWebRequest.Headers.Add出错 指定的值含有无效的控制字符
- 求一个抽签系统的代码?
- Devpress LayoutControl 隐藏右键菜单
- 推荐一个好的查找代理的工具,顺便散分~~~~~~~~~~~
- 用TcpClient做服务端来接受消息,并回复响应的问题。
- 恳请大家看看这个C#中调用VC编写的DLL的问题。(给了最高分100分,我只能给这么多分了))
#endregion #region 序列化和反序列化 /// <summary>
/// 从二进制数组反序列化得到对象
/// </summary>
/// <param name="buf">字节数组</param>
/// <returns>得到的对象</returns>
public static object DeserializeBinary(byte[] buf)
{
try
{
System.IO.MemoryStream memStream = new MemoryStream(buf);
memStream.Position=0;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
object newobj = deserializer.Deserialize(memStream);
memStream.Close();
return newobj;
}
catch
{
//ShowMsg(e.ToString());
return null;
}
} /// <summary>
/// 序列化为二进制字节数组
/// </summary>
/// <param name="request">要序列化的对象</param>
/// <returns>字节数组</returns>
public static byte[] SerializeBinary(object request)
{
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
serializer.Serialize(memStream, request);
return memStream.GetBuffer();
}
catch
{
//ShowMsg(e.ToString());
return null;
}
} #endregion