2016-05-02 1 views
0

Salut, j'ai les classes suivantes:DataContractSerializer exception throw sérialisation 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()); 
    } 
} 

Lorsque je tente de sérialiser l'objet que je reçois l'erreur suivante

Une exception non gérée du type « System.Runtime. Serialization.SerializationException 's'est produite dans System.Runtime.Serialization.dll

Informations supplémentaires: L'utilisation du type' Utils.Point2D 'comme une collection get-only n'est pas pris en charge avec NetDataContractSerializer. Envisagez en marquant le type avec l'attribut CollectionDataContractAttribute ou l'attribut SerializableAttribute ou en ajoutant un setter à la propriété .

Ce sujet se produit que lorsque je retire [IgnoreDataMember] pour l'échelle de la propriété dans le laserLineProfiler, et il ne se produit pas lorsque j'utilise la même Point2D dans la CameraCalibration2D de classe où l'objet CameraCalibration2D est une propriété de classe LaserLineProfiler il ne se plaint aucune raison pour laquelle je reçois cette erreur.

Merci

Répondre

0

j'ai utilisé la version 4.5 .NET et essayé d'exécuter le code et il fonctionne avec ou sans IgnoreDataMember. Je pense que vous n'avez pas affiché le code réel qui jette l'erreur. Jetez un oeil sur le lien pour la différence entre 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

Oui, ce n'est pas le code complet, j'essaie de comprendre ce qui provoque l'erreur –

+0

Oui, ce n'est pas le code complet, je tente de comprendre ce qui provoque l'erreur , Le laserLineProfile Implémenter une Interface IInterface et quand j'essaie d'obtenir l'objet m_CaptureDevice est l'instance de la classe qui implémente IInterface en utilisant le code suivant Assembly assy = Assembly.LoadFrom (Path.Combine (binDirectory, string.Format ("{0}. dll ", champs [0]))); ObjectHandle deviceHandle = Activateur.CreateInstance (assy.FullName, périphérique); m_CaptureDevice = deviceHandle.Unwrap(); –