Iman Kianrostami
Iman Kianrostami

Reputation: 502

serializing an object in C#

I have a serializable class named DataSource:

namespace GraphLib
{
public struct cPoint
{
    public float x;
    public float y;
}

[Serializable]
public class DataSource
{
    public delegate String OnDrawXAxisLabelEvent(DataSource src, int idx);
    public delegate String OnDrawYAxisLabelEvent(DataSource src, float value);

    public OnDrawXAxisLabelEvent OnRenderXAxisLabel = null;
    public OnDrawYAxisLabelEvent OnRenderYAxisLabel = null;

    private cPoint[] samples = null;

    private int length = 0;
    private String name = String.Empty;
    private int downSample = 1;
    private Color color = Color.Black;

    public float VisibleDataRange_X = 0;
    public float DY = 0;      
    public float YD0 = -200;
    public float YD1 = 200;
    public float Cur_YD0 = -200;
    public float Cur_YD1 = 200;

    public float grid_distance_y = 200;       // grid distance in units ( draw a horizontal line every 200 units )       

    public float off_Y = 0;
    public float grid_off_y = 0;

    public bool yFlip = true;      

    public bool Active = true;

    private bool YAutoScaleGraph = false;

    private bool XAutoScaleGraph = false;

    public float XAutoScaleOffset = 100;

    public float CurGraphHeight = 1.0f;

    public float CurGraphWidth = 1.0f;

    public float InitialGraphHeight = 0;

    public float InitialGraphWidth = 0;

    public bool AutoScaleY
    {
        get
        {
            return YAutoScaleGraph;
        }
        set
        {
            YAutoScaleGraph = value;
        }
    }

    public bool AutoScaleX
    {
        get
        {
            return XAutoScaleGraph;
        }
        set
        {
            XAutoScaleGraph = value;
        }
    }

    public cPoint[] Samples
    {
        get 
        {
            return samples; 
        }
        set 
        {
            samples = value;
            length = samples.Length;
        }
    }

    public float  XMin
    {
        get
        {
            float x_min = float.MaxValue;
            if (samples.Length > 0)
            {
                foreach (cPoint p in samples)
                {
                    if (p.x < x_min)  x_min=p.x;
                }
            }
            return x_min;
        }
    }

    public float XMax
    {
        get
        {
            float x_max = float.MinValue;
            if (samples.Length > 0)
            {
                foreach (cPoint p in samples)
                {
                    if (p.x > x_max) x_max = p.x;
                }
            }
            return x_max;
        }
    }

    public float YMin
    {
        get
        {
            float y_min = float.MaxValue;
            if (samples.Length > 0)
            {
                foreach (cPoint p in samples)
                {
                    if (p.y < y_min) y_min = p.y;
                }
            }
            return y_min;
        }
    }

    public float YMax
    {
        get
        {
            float y_max = float.MinValue;
            if (samples.Length > 0)
            {
                foreach (cPoint p in samples)
                {
                    if (p.y > y_max) y_max = p.y;
                }
            }
            return y_max;
        }
    }

    public void SetDisplayRangeY(float y_start, float y_end)
    {            
        YD0 = y_start;
        YD1 = y_end;
    }

    public void SetGridDistanceY(  float grid_dist_y_units)
    {
        grid_distance_y = grid_dist_y_units;
    }

    public void SetGridOriginY(  float off_y)
    {           
        grid_off_y = off_y;
    }

    [Category("Properties")] // Take this out, and you will soon have problems with serialization;
    [DefaultValue(typeof(string), "")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public String Name
    {
        get { return name; }
        set { name = value; }
    }

    [Category("Properties")] // Take this out, and you will soon have problems with serialization;
    [DefaultValue(typeof(Color), "")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color GraphColor
    {
        get { return color; }
        set { color = value; }
    }

    [Category("Properties")] // Take this out, and you will soon have problems with serialization;
    [DefaultValue(typeof(int), "0")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int Length
    {
        get { return length; }
        set 
        { 
            length = value;
            if (length != 0)
            {
                samples = new cPoint[length];
            }
            else
            {
                // length is 0
                if (samples != null)
                {
                    samples = null;
                }
            }
        }
    }

    [Category("Properties")] // Take this out, and you will soon have problems with serialization;
    [DefaultValue(typeof(int), "1")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int Downsampling
    {
        get { return downSample; }
        set { downSample = value; }
    }

} 
}

and i want to serialize it in a form like this:

public partial class Form1 : Form
{
    public GraphLib.PlotterDisplayEx display;
    private void serialize()
    {
        System.IO.Stream TestFileStream = System.IO.File.Create(@"C:\Users\Public\Documents\test.txt");
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        serializer.Serialize(TestFileStream, display.DataSources[0]);
        TestFileStream.Close();
    }

not that DataSource class that i want to serialize in Form1, is one of the attributes in GraphLib.PlotterDisplayEx class but when i run the program it gives me the following error:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

Additional information: Type 'KTK.Form1' in Assembly 'KTK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

UPDATE I updated the code for DataSource class.now it's complete code guys.

Upvotes: 1

Views: 554

Answers (1)

Henrik
Henrik

Reputation: 23324

You probably didn't show the complete code of the DataSource class. It directly or indirectly holds a reference to an object of type KTK.Form1. This might be through an event to which the form is subscribed. In this case you probably don't want to serialize it an should mark it as NonSerialized:

[field:NonSerialized]
public event ...;

Now that you updated the question. Change

public OnDrawXAxisLabelEvent OnRenderXAxisLabel = null;
public OnDrawYAxisLabelEvent OnRenderYAxisLabel = null;

to

[NonSerialized]
public OnDrawXAxisLabelEvent OnRenderXAxisLabel;
[NonSerialized]
public OnDrawYAxisLabelEvent OnRenderYAxisLabel;

The delegates may hold references to non-serializable classes.

Upvotes: 1

Related Questions