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()
{
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. The class is very similar to the SerialColor class. This did NOT fix the issue.
我将打开所有异常的中断,并在调试选项中关闭我的代码,然后运行序列化代码,看看是否看到一个异常,通常会吞下序列化
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.