2016-05-02 20 views
0

Cこんにちは、私は次のクラスがあります。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」タイプの未処理の例外を取得するには、オブジェクトをシリアル化してみてください。 System.Runtime.Serialization.dllでSerialization.SerializationException 'が発生しました

追加情報:取得専用の「Utils.Point2D」タイプの使用は、 コレクションではありませんNetDataContractSerializerでサポートされています。 CollectionDataContractAttribute属性を使用して型をマーキングするか、または SerializableAttribute属性を設定するか、セッターを プロパティに追加します。

IはlaserLineProfilerでプロパティScaleの[IgnoreDataMember]離陸、及びIはCameraCalibration2DオブジェクトがLaserLineProfilerクラスのプロパティであるクラスCameraCalibration2D内部同じたPoint2Dを使用する場合、それが発生しない場合にのみに発生されなぜこのエラーが出るのか不平を言うことはありません。

おかげ

答えて

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

はい、それは完全なコードではありません、私はエラーの原因となるかを把握しようとしている –

+0

はい、それは完全なコードではないですが、私はエラーの原因かを把握しようとしています、laserLineProfileはインターフェイスIInterfaceを実装し、m_CaptureDeviceオブジェクトを取得しようとすると、次のコードを使用してIInterfaceを実装するクラスのインスタンスAssembly Assy = Assembly.LoadFrom(Path.Combine(binDirectory、string.Format( "{0}。 dll "、fields [0]))); ObjectHandle deviceHandle = Activator.CreateInstance(assy.FullName、device); m_CaptureDevice = deviceHandle.Unwrap(); –

関連する問題