0

Hi I have the following classes :

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

When I try to serialize the object I get the following error

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

Additional information: The use of type 'Utils.Point2D' as a get-only collection is not supported with NetDataContractSerializer. Consider marking the type with the CollectionDataContractAttribute attribute or the SerializableAttribute attribute or adding a setter to the property.

This is only occur when I take off [IgnoreDataMember] for the Property Scale in the laserLineProfiler, and it does not occur when I use the same Point2d inside the class CameraCalibration2D where CameraCalibration2D object is a property of LaserLineProfiler class it does not complain any reason why I'm getting this error.

Thanks

J-P
  • 403
  • 1
  • 6
  • 21

1 Answers1

0

I used .NET 4.5 version and tried to run the code and it is working with or without IgnoreDataMember. I think you didn't post the actual code which is throwing the error. Have a look at the link for difference between for NetDCS vs 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());
        }
    }
}
Community
  • 1
  • 1
manu97
  • 7,237
  • 2
  • 17
  • 21
  • Yes it is not the full code, I'm trying to figure out what causing the error – J-P May 02 '16 at 19:54
  • Yes it is not the full code, I'm trying to figure out what causing the error, The laserLineProfile Implement an Interface IInterface and when I try to get m_CaptureDevice object is the instance of class which implement IInterface by using the following code Assembly assy = Assembly.LoadFrom(Path.Combine(binDirectory, string.Format("{0}.dll", fields[0]))); ObjectHandle deviceHandle = Activator.CreateInstance(assy.FullName, device); m_CaptureDevice = deviceHandle.Unwrap(); – J-P May 02 '16 at 20:00