序列化List <foo>,其中Foo是ISerializable </foo>

时间:2014-03-29 04:47:37

标签: c# serialization collections

我正在尝试序列化List<Foo>其中Foo实现ISerializable,我想要遵循自己的序列化/反序列化例程,这些例程非常复杂,但工作正常。这是我走了多远:

[Serializable]
public class FooList : List<Foo>, ISerializable
{
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {

        Foo[] tempAr = ToArray();

        // How do I populate these?
        SerializationInfo[] infoAr;
        StreamingContext[] ctxtAr;

        info.AddValue("infoAr", infoAr, typeof(SerializationInfo[]));
        info.AddValue("ctxtAr", ctxtAr, typeof(StreamingContext[]));
    }

    public SerializableList(SerializationInfo info, StreamingContext ctxt)
    {
        SerializationInfo[] infoAr = (SerializationInfo[])info.GetValue("infoAr", typeof(SerializationInfo[]));
        StreamingContext[] ctxtAr = (StreamingContext[])info.GetValue("ctxtAr", typeof(StreamingContext[]));

        int n = infoAr.Length;

        for (int i = 0; i < n; i++)
            Add(new Foo(infoAr[i], ctxtAr[i]));
    }
}

基本上,我想以遵循类中的例程的方式序列化它,而不仅仅是二进制转换。

0 个答案:

没有答案