简体   繁体   English

无法在C#中序列化对象的2D数组

[英]Trouble Serializing a 2D array of objects in C#

I have a 2D array of objects. 我有2D对象数组。 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. 当我序列化它们的2d数组时,没有收到错误消息。 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. 但是,我在文本编辑器中打开了要序列化的文件,并注意到大约第10行之后它开始破坏数据。 The array is 50 x 50 in size. 数组大小为50 x 50。 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 异常是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 和SerialColor类

[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()
    {
        Color 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. 编辑:好的,我还创建了SerialRectangle类,因为我浏览了文档,发现Rectangle是一个结构。 The class is very similar to the SerialColor class. 该类与SerialColor类非常相似。 This did NOT fix the issue. 这不能解决问题。

我将打开所有异常的中断,并在调试选项中关闭我的代码,然后运行序列化代码,看看是否看到一个异常,通常会吞下序列化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM