繁体   English   中英

在C#中序列化对象

[英]serializing an object in C#

我有一个名为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类的代码。现在它是完整的代码专家。

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

[field:NonSerialized]
public event ...;

现在,您更新了问题。 更改

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

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

委托可以保存对不可序列化类的引用。

暂无
暂无

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

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