串行化 应该是 序列化。我调试时发现问题出现在一个自定义集合上,继承自 CollectionBase 的一个子类,其内部基本类型是一个 struct 类型。该集合能被序列化,而在反序列化此类型时出现以上异常。
我把此集合实例从要序列化的对象中设为 [NonSerialized],反序列化是成功的,但得到的对象就不完整了。请高手们指点一下迷津。
我把此集合实例从要序列化的对象中设为 [NonSerialized],反序列化是成功的,但得到的对象就不完整了。请高手们指点一下迷津。
解决方案 »
- 给控件添加 鼠标单击事件
- 一个生成C#实体的存储过程
- 关于C#的名称空间,不懂,什么是名称空间啊,我新手。。
- Composite UI Application Block 的问题
- 用C#写的非托管代码程序,有没有可能在没有装.net framework的机器上运行?
- p2p初学者编程 - 如何使用同一个TcpClient连接多个远程地址
- 条形码打印
- 在中文系统开发的程序界面拿到英文系统处变样?各位怎解决?
- 怎样隐藏FLASH右键??(高分求解,100)
- 我装了.net Framework SDK,但编译C#程序时出现下面的错误,请问是什么原因?谢谢!
- 急!c#中调用存储过程,有何数据类型对应sqlserver2000中的numeric?可以传值?
- 急问???如何弹出打开文件对话框,选择要用的文件,读出文件数据,然后将文件数据写入access数据库中,请写详细代码!!!
我真的没有办法将它们都表示出来。不过由于是 struct 是值类型,在进出集合时,肯定会有大量的装箱和拆箱操作,不过不要以为是这是低性能的程序设计,我利用的就是这一点,可在克隆集合时省掉大量的语句。如要对引用类型的子对象进行深度复制实在很麻烦。流程采用递归调用的贪婪算法,每一个步骤结束后都要进行回朔,由于此集合在运行时存储了与状态密切相关的数据,所以每一步都要进行深度克隆集合。在编写(深度) Clone 方法时,利用值类型在集合内进出时的装拆箱操作,其语句非常简洁且反而高效。这一点在流程方案已确定的情况,是不会更改了,除非证明确实失败且无其他选择。这可要回退到系统分析阶段方可解决的问题了,此项目已进行了三个多月,代码已累计达到六万多行了。采用二进制序列化实现的是整个项目的文件保存功能——保存计算作业(包含计算数据和计算结果(可无)的对象),集合内存储的是计算前必须的初始数据。所以不可忽略此集合的序列化。
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;public class Test
{
public static void Main()
{ SC sc = new SC( new SB( new SA(122,4545), "asdf"), 545);
Stream stream = File.Open("data.ccc", FileMode.Create, FileAccess.Write);
// SoapFormatter formatter = new SoapFormatter(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, sc);
stream.Close();
//Empties obj.
//gbc = null;
//Opens file "data.ccc" and deserializes the object from it.
stream = File.Open("data.ccc", FileMode.Open);
// formatter = new SoapFormatter(); formatter = new BinaryFormatter(); object bc = formatter.Deserialize(stream);
stream.Close(); Console.WriteLine(bc.ToString());
Console.Read();
} [Serializable]
public struct SA
{
int _s1,_s2;
public SA( int s1, int s2)
{
_s1 = s1;
_s2 = s2;
}
} [Serializable]
public struct SB
{
SA sa;
string ss;
public SB(SA sa, string s1)
{
this.sa = sa;
this.ss = s1;
}
} [Serializable]
public struct SC
{
SB sb;
int c;
public SC(SB sb, int c1)
{
this.sb = sb;
this.c = c1;
} }
}
那你就不要用struct了啊。??
你不觉得这样的struct太大了吗?
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;public class Test
{
public static void Main()
{ SC sc = new SC( new SB( new SA(122,4545), "asdf"), 545);
Stream stream = File.Open("data.ccc", FileMode.Create, FileAccess.Write);
// SoapFormatter formatter = new SoapFormatter(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, sc);
stream.Close();
//Empties obj.
//gbc = null;
//Opens file "data.ccc" and deserializes the object from it.
stream = File.Open("data.ccc", FileMode.Open);
// formatter = new SoapFormatter(); formatter = new BinaryFormatter(); object bc = formatter.Deserialize(stream);
stream.Close(); Console.WriteLine(bc.ToString());
Console.Read();
} [Serializable]
public struct SA:ISerializable
{
int _s1,_s2;
public SA( int s1, int s2)
{
_s1 = s1;
_s2 = s2;
}
public SA(SerializationInfo info, StreamingContext context)
{
this._s1 = (int)info.GetValue("s1", typeof(int));
this._s2 = (int)info.GetValue("s2", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("s1", this._s1);
info.AddValue("s2", this._s2);
}
} [Serializable]
public struct SB:ISerializable
{
SA sa;
string ss;
public SB(SA sa, string s1)
{
this.sa = sa;
this.ss = s1;
}
public SB(SerializationInfo info, StreamingContext context)
{
this.sa = (SA)info.GetValue("sa", typeof(SA));
this.ss = (string)info.GetValue("ss", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("sa", this.sa);
info.AddValue("ss", this.ss);
} } [Serializable]
public struct SC:ISerializable
{
SB sb;
int c;
public SC(SB sb, int c1)
{
this.sb = sb;
this.c = c1;
}
public SC(SerializationInfo info, StreamingContext context)
{
this.sb = (SB)info.GetValue("sb", typeof(SB));
this.c = (int)info.GetValue("c", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("sb", this.sb);
info.AddValue("c", this.c);
}
}
}
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q320079上面列了workaround,可以参考一下。
感谢知秋一叶,提供Bug Page。不过此异常解决之后,又发生了新的异常,请不吝再指教。另开帖,此帖先结。