DataContractSerializer序列化抛出异常c#

时间:2016-05-02 18:53:22

标签: c# exception properties datacontractserializer

您好我有以下课程:

public class Point2D
{

    public Point2D()
    {
        X = double.NaN;
        Y = double.NaN;
    }

    public Point2D(double xValue, double yValue)
    {
        X = xValue;
        Y = yValue;
    }

    /// <summary>
    /// The X coordinate of the point.
    /// </summary>
    public double X { get; set; }

    /// <summary>
    /// The Y coordiante of the point.
    /// </summary>
    public double Y { get; set; }
}

public class CameraCalibration2D
{

    private Point2D _GridOffset = new Point2D(0, 0);
    [Category("Grid Definition")]
    [Description("The location of the top left corner of the calibration grid in real world coordinates (mm).")]
    [DisplayName("Grid Offset")]
    public Point2D GridOffset
    {
        get { return _GridOffset; }
        set { _GridOffset = value; }
    }
}


public class LaserLineProfiler 
{
    public LaserLineProfiler()
    {

    }

    #region Configuration

    private CameraCalibration2D _Calibration2D = new CameraCalibration2D();
    [Category("Calibration")]
    [Description("The real world to pixel mapping calibration.")]
    [DisplayName("2D Calibration")]
    public CameraCalibration2D Calibration2D
    {
        get { return _Calibration2D; }
        set { _Calibration2D = value; }
    }

    private Point2D _Scale = new Point2D(0, 0);
    //[IgnoreDataMember]
    [Category("Laser Line")]
    [Description("The length, in world units, of one pixel, in the X and Y-direction")]
    public Point2D Scale 
    { 
        get{return _Scale;} 
        set{_Scale = value;} 
    }

public partial class PageDeviceEditor : UserControl
{

    LaserLineProfiler m_CaptureDevice = new LaserLineProfiler() ;
    public PageDeviceEditor()
    {
        InitializeComponent();
    }



    private void buttonAddDevice_Click(object sender, EventArgs e)
    {

     propertyGridDeviceConfig.SelectedObject = m_CaptureDevice   
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Console.WriteLine(SerializeCalDataObject.ToXML(m_CaptureDevice));
    }
}
  public static class SerializeCalDataObject
  {
    public static XElement ToXML(this object o)
    {
        Type t = o.GetType();

        DataContractSerializer serializer = new DataContractSerializer(t);
        StringWriter sw = new StringWriter();
        XmlTextWriter xw = new XmlTextWriter(sw);
        serializer.WriteObject(xw, o);
        return XElement.Parse(sw.ToString());
    }
}

当我尝试序列化对象时,我收到以下错误

  

System.Runtime.Serialization.dll中发生未处理的“System.Runtime.Serialization.SerializationException”类型异常

     

其他信息:使用“Utils.Point2D”类型作为只读   NetDataContractSerializer不支持集合。考虑   使用CollectionDataContractAttribute属性标记类型或   SerializableAttribute属性或添加一个setter   属性。

这只发生在我为laserLineProfiler中的Property Scale取消[IgnoreDataMember]时,并且当我在CameraCalibration2D类中使用相同的Point2d时,它不会发生,其中CameraCalibration2D对象是LaserLineProfiler类的属性,它不会抱怨我收到此错误的任何原因。

由于

1 个答案:

答案 0 :(得分:0)

我使用的是.NET 4.5版本并尝试运行代码,无论是否使用IgnoreDataMember都可以使用。我认为你没有发布导致错误的实际代码。查看NetDCS与DataContractSerializer之间差异的链接。

NetDataContractSerializer vs DataContractSerializer

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    public class Point2D
    {
        public Point2D()
        {
            X = double.NaN;
            Y = double.NaN;
        }

        public Point2D(double xValue, double yValue)
        {
            X = xValue;
            Y = yValue;
        }

        public double X { get; set; }
        public double Y { get; set; }
    }

    public class CameraCalibration2D
    {

        private Point2D _GridOffset = new Point2D(0, 0);
        public Point2D GridOffset
        {
            get { return _GridOffset; }
            set { _GridOffset = value; }
        }
    }

    public class LaserLineProfiler 
    {
        public LaserLineProfiler()
        {

        }

        private CameraCalibration2D _Calibration2D = new CameraCalibration2D();
        public CameraCalibration2D Calibration2D
        {
            get { return _Calibration2D; }
            set { _Calibration2D = value; }
        }

        private Point2D _Scale = new Point2D(0, 0);
        [IgnoreDataMember]    
        public Point2D Scale 
        { 
            get{return _Scale;} 
            set{_Scale = value;} 
        }

        public partial class PageDeviceEditor
        {

            static LaserLineProfiler m_CaptureDevice = new LaserLineProfiler() ;
            public PageDeviceEditor()
            {

            }

            static void Main(string[] args)
            {
                Console.WriteLine(SerializeCalDataObject.ToXML(m_CaptureDevice));
                Console.ReadLine();
            }
        }
    }

    public static class SerializeCalDataObject
    {
        public static XElement ToXML(this object o)
        {
            Type t = o.GetType();

            DataContractSerializer serializer = new DataContractSerializer(t);
            StringWriter sw = new StringWriter();
            XmlTextWriter xw = new XmlTextWriter(sw);
            serializer.WriteObject(xw, o);
            return XElement.Parse(sw.ToString());
        }
    }
}