2016-05-02 16 views
0

Hallo Ich habe die folgenden Klassen:DataContractSerializer Serialisierung throw Ausnahme C#

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

Wenn ich versuche, das Objekt ich folgende Fehler

Eine nicht behandelte Ausnahme des Typs ‚System.Runtime erhalten zu serialisiert. Serialization.SerializationException 'in System.Runtime.Serialization.dll aufgetreten

Weitere Informationen: Die Verwendung von Typ' Utils.Point2D 'als eine Get-only Sammlung ist nicht unterstützt mit NetDataContractSerializer. Betrachten Sie , indem Sie den Typ mit dem CollectionDataContractAttribute-Attribut oder das SerializableAttribute-Attribut markieren oder der -Eigenschaft einen Setter hinzufügen.

Dies wird nur auftreten, wenn ich abhebe [IgnoreDataMember] für die Property-Skala in dem laserLineProfiler, und es tritt nicht auf, wenn ich die gleiche Point2D innerhalb der Klasse CameraCalibration2D verwenden, wo CameraCalibration2D Objekt eine Eigenschaft von LaserLineProfiler Klasse ist es beklagt keinen Grund, warum ich diesen Fehler bekomme.

Dank

Antwort

0

ich mit oder ohne IgnoreDataMember .NET 4.5-Version und versucht, den Code auszuführen, und es funktioniert. Ich denke, du hast den eigentlichen Code nicht gepostet, der den Fehler verursacht. Werfen Sie einen Blick auf den Link für den Unterschied zwischen 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()); 
     } 
    } 
} 
+0

Ja es nicht der vollständige Code ist, ich versuche, herauszufinden, was den Fehler verursacht –

+0

Ja, es ist nicht der vollständige Code, ich versuche, was den Fehler verursacht, um herauszufinden, Das laserLineProfile Implementiert ein Interface IIinterface und wenn ich versuche, das m_CaptureDevice Objekt zu bekommen, ist dies die Instanz der Klasse, die IInterface implementiert, indem sie den folgenden Code verwendet Assy = Assembly.LoadFrom (Path.Combine (binDirectory, string.Format ("{0}. dll ", Felder [0]))); ObjectHandle deviceHandle = Activator.CreateInstance (assy.FullName, device); m_CaptureDevice = deviceHandle.Unwrap(); –

Verwandte Themen