dimanche 27 septembre 2015

One parameter of custom class is re-written

I have a custom class for triangles:

[XmlRootAttribute(Namespace = "", IsNullable = false)]
[XmlType("Figure.Triangle")]
public class Triangle : Figure
{
    [XmlIgnore]
    public Point a { get; set; }
    [XmlIgnore]
    public Point b { get; set; }
    [XmlIgnore]
    public Point c { get; set; }

    [XmlElement("a")]
    public string aString
    {
        get { return a.X.ToString() + ';' + a.Y.ToString(); }
        set
        {
            if (String.IsNullOrWhiteSpace(value))
                return;
            string[] xmlArr = value.Split(';');
            this.a = new Point(Convert.ToInt32(xmlArr[0]), Convert.ToInt32(xmlArr[1]));
        }
    }

    [XmlElement("b")]
    public string bString
    {
        get { return b.X.ToString() + ';' + b.Y.ToString(); }
        set
        {
            if (String.IsNullOrWhiteSpace(value))
                return;
            string[] xmlArr = value.Split(';');
            this.b = new Point(Convert.ToInt32(xmlArr[0]), Convert.ToInt32(xmlArr[1]));
        }
    }

    [XmlElement("c")]
    public string cString
    {
        get { return c.X.ToString() + ';' + c.Y.ToString(); }
        set
        {
            if (String.IsNullOrWhiteSpace(value))
                return;
            string[] xmlArr = value.Split(';');
            this.c = new Point(Convert.ToInt32(xmlArr[0]), Convert.ToInt32(xmlArr[1]));
        }
    }

    [XmlIgnore]
    public Pen pen { get; set; }
    [XmlElement("PenColor")]
    public int penColor
    {
        get { return pen.Color.ToArgb(); }
        set { this.pen.Color = Color.FromArgb(value); }
    }

    [XmlElement("PenWidth")]
    public float penWidth
    {
        get { return this.pen.Width; }
        set { this.pen.Width = value; }
    }

    [XmlIgnore]
    public SolidBrush brush { get; set; }
    [XmlElement("BrushColor")]
    public int brushColor
    { 
        get { return this.brush.Color.ToArgb();}
        set { this.brush.Color = Color.FromArgb(value); }
    }
    public Triangle()
    {
        a = new Point(0, 0);
        b = new Point(0, 0);
        c = new Point(0, 0);
        pen = new Pen(Color.Black, 1);
    }

    public Triangle(Point a1, Point b1, Point c1, Pen myPen)
    {
        this.a = a1;
        this.b = b1;
        this.c = c1;
        this.pen = myPen;
    }
}

I serialize my figures to the xml-structure and save them. If I need I deserialize them back and re-draw in the pictureBox. The issue is: when I deserialize figures from xml, all filled figures have the color of the last one.

Here's part of deserialization:

foreach (XmlNode singleNode in nodes)
{
    Type TestType = GetTypeFromAssemblyByName(singleNode.Attributes.GetNamedItem("d1p1:type").Value);
    if (TestType != null)
    {
        ConstructorInfo ci = TestType.GetConstructor(new Type[] { });
        object Obj = ci.Invoke(new object[] { });
        MethodInfo method = TestType.GetMethod("Deserialize");
        object result = method.Invoke(Obj, new object[] { singleNode.OuterXml });                      
        listObjects.Add(result);
    }
    else
    {
        Console.WriteLine("Class wasn't found");
    }
}

E.g. I have 2 filled triangles which are saved to Xml. First is blue and second is red and both of them have different brush color, which is need for fillPolygon method.

<Workspace>
  <Figure d1p1:type="Figure.FilledTriangle" xmlns:d1p1="http://ift.tt/ra1lAU">
    <a>64;68</a>
    <b>96;295</b>
    <c>283;41</c>
    <PenColor>-16777216</PenColor>
    <PenWidth>1</PenWidth>
    <BrushColor>-16744193</BrushColor>
  </Figure>
  <Figure d1p1:type="Figure.FilledTriangle" xmlns:d1p1="http://ift.tt/ra1lAU">
    <a>321;411</a>
    <b>575;152</b>
    <c>629;462</c>
    <PenColor>-16777216</PenColor>
    <PenWidth>1</PenWidth>
    <BrushColor>-65408</BrushColor>
  </Figure>
</Workspace>

Everything seems right, but when I invoke the Deserialize method, the result is, that the brush color of all previously added figures in the list is re-written. So in my example I have two red triangles on theirs right positions.

I understand that list contains references to the result, but other parameters: coordinates, pen color, etc are left as they were.

How can I get rid of this? I've tried to write result values to array, but even in the array they were replaced.





Aucun commentaire:

Enregistrer un commentaire