Question

I have a class X which implements ISerializable

#region ISerializable Members

    /// <summary>
    /// Sets up for deserialization
    /// </summary>
    /// <param name="info"></param>
    /// <param name="context"></param>
    private X(SerializationInfo info, StreamingContext context)
    {
        this.Key = info.GetString("Key");
        this.Title = info.GetString("Title");
        this.Count = info.GetInt32("Count");

        originalDataSource = new ObservableDataSource<IDataType>((IDataType[])info.GetValue("DataSource", typeof(IDataType[])));

        this.A = (Color)ColorConverter.ConvertFromString(info.GetString("A"));
        this.B = Utilities.GetDashStyleFromString(info.GetString("B"));
        this.C = info.GetDouble("C");
        this.D = (Color)ColorConverter.ConvertFromString(info.GetString("D"));
        this.E = (Shape)Enum.Parse(typeof(Shape), info.GetString("E"));
        this.F = info.GetInt32("F");
        this.G = info.GetInt32("G");
        this.H = info.GetInt32("H");
        this.I = info.GetBoolean("I");
    }

    /// <summary>
    /// Sets up for serialization
    /// </summary>
    /// <param name="info"></param>
    /// <param name="context"></param>
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Key", this.Key);
        info.AddValue("Title", this.Title);
        info.AddValue("Count", this.Count);

        info.AddValue("DataSource", this.originalDataSource.Collection.ToArray<IDataType>());

        info.AddValue("A", this.A.ToString());
        info.AddValue("B", this.B.ToString());
        info.AddValue("C", this.C);
        info.AddValue("D", this.D.ToString());
        info.AddValue("E", this.E);
        info.AddValue("F", this.F);

        info.AddValue("G", this.G);
        info.AddValue("H", this.H);
        info.AddValue("I", this.I);
    }

    #endregion

I have a class Y which contains a List and an eventaggregator.

#region ISerializable Members

    /// <summary>
    /// Sets up for deserialization
    /// </summary>
    /// <param name="info"></param>
    /// <param name="context"></param>
    private Y(SerializationInfo info, StreamingContext context)
    {
        List<X> Coll = (List<X>)info.GetValue("DataSource", typeof(List<X>));

        if (_Collection == null)
            _Collection = new List<X>();

        foreach (X x in Coll)
            _Collection.Add(x);

        Count = info.GetInt32("Count");
    }

    /// <summary>
    /// Sets up for serialization
    /// </summary>
    /// <param name="info"></param>
    /// <param name="context"></param>
    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("DataSource", _Collection);
        info.AddValue("Count", this.Count);
    }

    #endregion

I need to serialize Y in binary.

But when I deserialize Y, and check the following line, it returns a List with nulls in place of the X objects that were supposed to have been serialized.

List<X> Coll = (List<X>)info.GetValue("DataSource", typeof(List<X>));

Any idea what is wrong with this piece of code? I am unable to simply serialize with conditional serialization, the Color does not get serialized and also, the ObservableDataSource does not have Serializable attribute and I cannot modify it.

This is in .NET framework 3.5 (if that is of any use).

Was it helpful?

Solution

The Color class is not serializable, you will have to implement your own custom serialization format for it (I usually, serialize the ARGB code instead).

Except from that, the BinarySerializer supports collection serialization so you don't actually need to implement your own serialization function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top