反序列化发生的奇怪问题

wwwiii520 2011-10-18 06:33:22

namespace WangDa.LeasedLine.WinForm.Print
{
/// <summary>
///保存设计器中的信息
///在保存文件的时候反它序列化后保存
/// </summary>
[Serializable()]
public class ObjectSerializer
{
/// <summary>
/// 要将设计的对像保存到文件的信息
/// </summary>
/// <param name="objectSave">设计器中的对像列表</param>
/// <param name="width">设计器的宽度</param>
/// <param name="height">设计器的高度</param>
/// <param name="pconfig">打印设置</param>
public ObjectSerializer(DrawItemList objectSave, int width, int height, PrintConfig pconfig, List<PrintItem> printItems, int rowcount)
{
itemList = objectSave;
des_W = width;
des_H = height;
printconfig = pconfig;
_printItems = printItems;
_rowcount = rowcount;
}

private DrawItemList itemList;
private int des_W, des_H, _rowcount;
private PrintConfig printconfig;
private List<PrintItem> _printItems;
/// <summary>
/// 取得或设置每页有多少行
/// </summary>
public int RowCount
{
get
{
return _rowcount;
}
set
{
_rowcount = value;
}
}
/// <summary>
/// 取得或设计设计器中的对像列表
/// </summary>
public DrawItemList ItemList
{
get
{
return itemList;
}
set
{
itemList = value;
}
}

/// <summary>
/// 取得或设置设计器的宽度
/// </summary>
public int Width
{
get
{
return des_W;
}
set
{
des_W = value;
}
}

/// <summary>
/// 取得或设置设计器的高度
/// </summary>
public int Height
{
get
{
return des_H;
}
set
{
des_H = value;
}
}

/// <summary>
/// 存储打印设置
/// </summary>
public PrintConfig PrinterConfig
{
get
{
return printconfig;
}
set
{
printconfig = value;
}
}
/// <summary>
/// 存储打印列表
/// </summary>
public List<PrintItem> PrintItems
{
get
{
return _printItems;
}
set
{
_printItems = value;
}
}
}
}





------------------------------------------------------------------------------------------------


namespace WangDa.LeasedLine.WinForm.Print
{
[Serializable()]
public class PrintItem : ISerializable
{
public PrintItem()
{ }

public bool Check
{
get;
set;
}
public string ColumnName
{
get;
set;
}
public string Caption
{
get;
set;
}
public object DrawItem
{
get;
set;
}

new public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Check", this.Check);
info.AddValue("ColumnName", this.ColumnName);
info.AddValue("Caption", Caption);
info.AddValue("DrawItem", DrawItem);
}

public PrintItem(SerializationInfo info, StreamingContext context)
: this()
{
Check = (bool)info.GetValue("Check", typeof(bool));
ColumnName = info.GetString("ColumnName");
ColumnName = info.GetString("Caption");
ColumnName = info.GetString("DrawItem");
}
}

====================================================================================================

反序列化ObjectSerializer老是提示
{"无法加载进行反序列化所需的类型 System.Collections.Generic.List`1[[WangDa.LeasedLine.WinForm.Print.PrintItem, WangDa.LeasedLine.WinForm.Print, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]。"}


开发平台用的是.net4.0 原来以为 List<T>不支持序列化 但我现在不管有没有实现ISerializable都不行
...全文
211 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwiii520 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sp1234 的回复:]

在手工进行序列化时,把你的 printItems 数据用数组来保存,而不是List<>。
[/Quote]

刚试了下,改数组果然成功了,我想过好多次用数组的,就是没试.
wwwiii520 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sp1234 的回复:]

在手工进行序列化时,把你的 printItems 数据用数组来保存,而不是List<>。
[/Quote]

List<T>支持序列化的 .net4.0
  • 打赏
  • 举报
回复
在手工进行序列化时,把你的 printItems 数据用数组来保存,而不是List<>。
wwwiii520 2011-10-18
  • 打赏
  • 举报
回复
求大神啊
wwwiii520 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wwwiii520 的回复:]
还有一点 我用filestream对像序列化成文件,然后打开这个问题都是没有问题的
现在我是存byte[]到数据库的,就有问题了
[/Quote]

数据库我的是image型的

没有问题的了,我看了存是byte[].lenth和取出来是一样的。
人工智能算法 2011-10-18
  • 打赏
  • 举报
回复
存到数据库的时候出了问题吧?
数据库设计是什么数据类型存储的?
wwwiii520 2011-10-18
  • 打赏
  • 举报
回复
还有一点 我用filestream对像序列化成文件,然后打开这个问题都是没有问题的
现在我是存byte[]到数据库的,就有问题了
wwwiii520 2011-10-18
  • 打赏
  • 举报
回复


[code=C#]
namespace WangDa.LeasedLine.WinForm.Print.IO
{
public class InOutPut
{
public class UBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
// 取得当前的程序集名称,取得传递过来,类型名称不修改,都是"TestPro.CSFTcpMsg"
string strCurAssembly = Assembly.GetExecutingAssembly().FullName;

// 返回当前对应的类型
Type CurType = Type.GetType(String.Format("{0}, {1}", typeName, strCurAssembly));
return CurType;
}
}

/// <summary>
/// 将设计器中的对像列表保存到文件中
/// </summary>
/// <param name="designer">所要保存的设计器</param>
/// <param name="fileName">要保存的文件名称</param>
public static byte[] SaveFile(Designer designer)
{
ObjectSerializer osave = new ObjectSerializer(designer.Items, designer.Width, designer.Height, designer.PrinterConfig, designer.PrintItems, designer.PageRows);

return SerializableObj<ObjectSerializer>.Serialize(osave);
}

/// <summary>
/// 打开保存的文件
/// 在打开保存的文件,此函数会返回保存的类
/// 在将类的数据返回给设计器的时候需刷新
/// 设计器和放在设计器的容器
/// 刷新设计器是将对像显示出来
/// 刷新设计器容器是重绘标尺
/// </summary>
/// <param name="file">打开的文件名称</param>
/// <returns>文件是否成功打开</returns>
public static bool OpenFile(Designer designer, byte[] fs)
{
bool opensuccess = false;
ObjectSerializer saveObject;
try
{
object o = (object)Deserizlize<ObjectSerializer>.Deserilize(fs);
saveObject = Deserizlize<ObjectSerializer>.Deserilize(fs) as ObjectSerializer;

designer.Items = saveObject.ItemList;
designer.Width = saveObject.Width;
designer.Height = saveObject.Height;
designer.PrinterConfig = saveObject.PrinterConfig;
designer.PrintItems = saveObject.PrintItems;
designer.PageRows = saveObject.RowCount;
designer.Refresh();
opensuccess = true;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "出错提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
//formatter = null;
return opensuccess;
}

/// <summary>
/// 二进制序列化
/// </summary>
public class SerializableObj<T>
{
public static Byte[] Serialize(T obj)
{
try
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);
ms.Position = 0;
byte[] buffers = ms.ToArray();
ms.Close();
ms.Dispose();
return Compress(buffers);
}
catch (Exception ex)
{
throw ex;
}
}

public static byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true);
gzip.Write(buffer, 0, buffer.Length);
gzip.Close();
ms.Position = 0;
byte[] rebuffer = new byte[ms.Length];
ms.Read(rebuffer, 0, int.Parse(ms.Length.ToString()));
return rebuffer;
}
}

/// <summary>
/// 二进制反序列化
/// </summary>
public class Deserizlize<T>
{
public static byte[] Decompress(byte[] buffer)
{
try
{
MemoryStream ms = new MemoryStream(buffer);
ms.Position = 0;
GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);
byte[] rebuffer = new byte[100];
MemoryStream msre = new MemoryStream();
int readcont = gzip.Read(rebuffer, 0, rebuffer.Length);
while (readcont > 0)
{
msre.Write(rebuffer, 0, readcont);
readcont = gzip.Read(rebuffer, 0, rebuffer.Length);
}
return msre.ToArray();
}
catch (Exception ex)
{

throw ex;
}
}

public static object Deserilize(byte[] buffer)
{
try
{
byte[] debuffer = Decompress(buffer);
BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new UBinder();
MemoryStream ms = new MemoryStream(debuffer);
ms.Position = 0;
object obj = (object)bf.Deserialize(ms);
return obj;
}
catch (Exception ex)
{

throw ex;
}
}
}
}


[/code]

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧