以下代码在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'.
有什么想法吗?
答案 0 :(得分:1)
我不知道问题的原因是什么,但我找到了解决问题的方法:
替换此行
XmlSerializer serializer = new XmlSerializer(typeof(PointD));
有这样的事情:
XmlSerializer serializer = new XmlSerializer(typeof(PointD), new Type[]{typeof(Point)});
答案 1 :(得分:0)
确保您的程序集和引用的System.Drawing程序集都具有相同的.net版本。当启动程序集设置为.NET framework 4 Client Profile时,我看到了这个错误,并且ref'd程序集设置为.NET framework 4(右键单击,属性,应用程序)