串行化 应该是 序列化。我调试时发现问题出现在一个自定义集合上,继承自 CollectionBase 的一个子类,其内部基本类型是一个 struct 类型。该集合能被序列化,而在反序列化此类型时出现以上异常。
我把此集合实例从要序列化的对象中设为 [NonSerialized],反序列化是成功的,但得到的对象就不完整了。请高手们指点一下迷津。

解决方案 »

  1.   

    把你的struct的定义POST出来吧。
      

  2.   

    struct 内还有其他 struct 成员,每个 struct 类型都有大量必要的方法和操作符重载。不过除了 string 引用类型的成员外,并不包含其他 ValueType 类型的成员。
    我真的没有办法将它们都表示出来。不过由于是 struct 是值类型,在进出集合时,肯定会有大量的装箱和拆箱操作,不过不要以为是这是低性能的程序设计,我利用的就是这一点,可在克隆集合时省掉大量的语句。如要对引用类型的子对象进行深度复制实在很麻烦。流程采用递归调用的贪婪算法,每一个步骤结束后都要进行回朔,由于此集合在运行时存储了与状态密切相关的数据,所以每一步都要进行深度克隆集合。在编写(深度) Clone 方法时,利用值类型在集合内进出时的装拆箱操作,其语句非常简洁且反而高效。这一点在流程方案已确定的情况,是不会更改了,除非证明确实失败且无其他选择。这可要回退到系统分析阶段方可解决的问题了,此项目已进行了三个多月,代码已累计达到六万多行了。采用二进制序列化实现的是整个项目的文件保存功能——保存计算作业(包含计算数据和计算结果(可无)的对象),集合内存储的是计算前必须的初始数据。所以不可忽略此集合的序列化。
      

  3.   

    更正一下,每个 struct 内并没有包含除了 string 以外的其他引用类型 ReferenceType 成员。
      

  4.   

    你的问题没有碰到过,只能提一些建议。struct以及它内部的其他struct成员实现的是浅表复制还是深度复制,如果是浅表复制,为它们实现深度复制看看。
      

  5.   

    我在一个测试工程使用了以下代码,来还原我的错误,以下代码运行时引发此类异常。
    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;
    } }
    }
      

  6.   

    异常不是发生在集合上,而是嵌套 struct 的第三层上,即 SC 上,对 SB 及 SA 进行二进制序列化操作不引发此异常。
      

  7.   

    恩。。我也看到了。
    那你就不要用struct了啊。??
    你不觉得这样的struct太大了吗?
      

  8.   

    为对象实现ISerializable接口,控制对象自己的序列化和反序列化过程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: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);
    }
    }
    }
      

  9.   

    Bug,就是这样发生的.... :)
    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q320079上面列了workaround,可以参考一下。
      

  10.   

    感谢芝麻开门,提供解决方法。
    感谢知秋一叶,提供Bug Page。不过此异常解决之后,又发生了新的异常,请不吝再指教。另开帖,此帖先结。