1

以下代码会在 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'.

有任何想法吗?

4

2 回答 2

1

我不知道问题的原因是什么,但我找到了解决它的方法:

替换此行

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

像这样:

XmlSerializer serializer = new XmlSerializer(typeof(PointD), new Type[]{typeof(Point)});
于 2012-09-25T21:32:13.497 回答
0

确保您的程序集和引用的 System.Drawing 程序集都具有相同的 .net 版本。当启动程序集设置为 .NET framework 4 Client Profile 并且 ref'd 程序集设置为 .NET framework 4(右键单击、属性、应用程序)时,我已经看到此错误

于 2012-09-25T18:47:36.287 回答