簡體   English   中英

Xml序列化失敗與隱式類型轉換器

[英]Xml Serialization Failure with implicit type converter

以下代碼在Windows 7而非Windows 8上引發運行時錯誤。

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

  public static implicit operator PointD(Point point)
  {
    return new PointD() { X = point.X, Y = point.Y };
  }
}

var p = new PointD();
XmlSerializer serializer = new XmlSerializer(typeof(PointD));
using (var stream = File.Create("test.xml"))
  serializer.Serialize(stream, p);

錯誤是:

Unable to generate a temporary class (result=1).
error CS0012: The type 'System.Drawing.Point' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Drawing, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

有任何想法嗎?

我不知道是什么原因造成的,但是我找到了一種解決方法:

替換此行

XmlSerializer serializer = new XmlSerializer(typeof(PointD));

像這樣:

XmlSerializer serializer = new XmlSerializer(typeof(PointD), new Type[]{typeof(Point)});

確保您的程序集和引用的System.Drawing程序集都具有相同的.net版本。 當啟動程序集設置為.NET Framework 4 Client Profile,並且引用程序集設置為.NET Framework 4(右鍵單擊,屬性,應用程序)時,我已經看到此錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM