在C#中序列化对象

时间:2016-02-09 10:37:23

标签: c# serialization

我有一个名为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; }
    }

} 
}

我希望以这样的形式序列化它:

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();
    }

我想在 Form1 中序列化的 DataSource 类不是 GraphLib.PlotterDisplayEx 类中的属性之一 但是当我运行程序时它会给我以下错误:

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.

更新 我更新了DataSource类的代码。现在它是完整的代码。

1 个答案:

答案 0 :(得分:1)

您可能没有显示DataSource类的完整代码。它直接或间接地保存对KTK.Form1类型的对象的引用。 这可能是通过订阅表单的事件。 在这种情况下,您可能不希望序列化它,并将其标记为NonSerialized

[field:NonSerialized]
public event ...;

现在您已更新了问题。变化

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

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

代理人可以保留对不可序列化类的引用。

相关问题