开发者

Trouble Serializing a 2D array of objects in C#

I have a 2D array of objects. The hierarchy looks like this:

PixelData : ISerializable
-Rectangle
-SerialColor

SerialColor : ISerializable
-R
-G
-B

When I serialize the 2d array of them, I get no error message. However, I open the file that it serializes in a text editor and notice that after about the 10th row it starts to corrupt the data. The array is 50 x 50 in size. I have tried using different techniques to serialize the objects. Again no error message occurs when serializing the data. When I try to deserialize the data I get terrible exception. What should I do?

The exception is System.Reflection.TargetInvocationException: exception has been thrown by the target of an invocation

[Serializable()]
public class PixelData : ISerializable
{
    #region Constructors
    public PixelData()
        : this(new Rectangle(0, 0, 0, 0), Color.White)
    {

    }
    public PixelData(Rectangle r, Color c)
    {
        this.BoundingRect = r;
        this.CellColor = c;
    }
    public PixelData(SerializationInfo info, StreamingContext ctxt)
    {
        this.BoundingRect = (Rectangle)info.GetValue("BoundingRect", typeof(Rectangle));
        SerialColor c = (SerialColor)info.GetValue("CellColor", typeof(SerialColor));
        this.CellColor = c.GetColor();
    }
    #endregion
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("BoundingRect", BoundingRect);
        info.AddValue("CellColor", new SerialColor(CellColor));
    }
    #endregion
    #region Properties
    public Rectangle BoundingRect
    {
        get;
        set;
    }
    public Color CellColor
    {
        get;
        set;
    }
    #endregion
}

and the SerialColor class

[Serializable()]
public class SerialColor : ISerializable
{

    public SerialColor()
        : this(0, 0, 0)
    {

    }

    public SerialColor(int r, int g, int b)
    {
        R = r;
        G = g;
        B = b;
    }

    public SerialColor(Color c)
    {
        R = c.R;
        G = c.G;
        B = c.B;
    }

    public SerialColor(SerializationInfo info, StreamingContext ctxt)
    {
        R = (int)info.GetValue("R", typeof(int));
        G = (int)info.GetValue("G", typeof(int));
        B = (int)info.GetValue("B", typeof(int));
    }
    #endregion
    #region Methods

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("R", R);
        info.AddValue("G", G);
        info.AddValue("B", B);
    }

    public Color GetColor()
    {
        Color c =  Color.FromArgb(R, G, B);
        try
        {
            //this will throw an exception if the name of the color is not hexadecimal
            Int32.Parse(c.Name, System.Globalization.NumberStyles.HexNumber);
            //attempt to create a non-hex name for the color
            return ColorTranslator.FromHtml("#" + c.Name);
        }
        catch (Exception e)
        {
            //returns the name if it is not hexadecimal
            return c;
        }
    }

    public override string ToString()
    {
        Col开发者_开发百科or c = this.GetColor();
        return c.Name + "[ " + c.R.ToString() + " , " + c.G.ToString() + " , " + c.B.ToString() + " ]";
    }

    public override bool Equals(object obj)
    {
        if (!(obj is SerialColor))
            return false;

        SerialColor other = (SerialColor)obj;
        return ((this.R == other.R) && (this.G == other.G) && (this.B == other.B));
    }

    public override int GetHashCode()
    {
        return this.GetColor().GetHashCode();
    }
    #endregion
    #region Properties

    public int R
    {
        get;
        set;
    }

    public int G
    {
        get;
        set;
    }

    public int B
    {
        get;
        set;
    }
    #endregion
}

EDIT: OK I also created a SerialRectangle class because I browsed through the documentation and found that Rectangle is a struct. The class is very similar to the SerialColor class. This did NOT fix the issue.


I would turn on break on all exceptions and in debugging options turn off just my code then run serialize code and see if it see's an exception that the serialization normally swallows

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜