gpt4 book ai didi

c# - 没有无参数构造函数的子类的 XML 序列化

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:58 25 4
gpt4 key购买 nike

我正在尝试使用另一个可序列化类的无参数构造函数来序列化一个子类。由于没有无参数构造函数,无论我如何尝试,我总是得到 InvalidOperationException。

我试图将我的子类转换为基类,同时使用:simlpe 转换(通过简单转换,我的意思是在括号之间插入所需类型),以及 Convert.ChangeType(...)。虽然前者不起作用(我仍然得到异常),但后一种方法会导致 InvalidCastException(消息说该对象必须实现 IConvertible 接口(interface))。

这是完全可序列化的基类:

 [XmlRoot("NdSRD_Environment")]
public class Environment
{
#pragma warning disable IDE1006 // Naming Styles
[XmlAttribute]
public string id { get; set; }
[XmlElement]
public double realWidth { get; set; }
[XmlElement]
public double realHeight { get; set; }
[XmlElement]
public double realDepth { get; set; }
[XmlElement]
public int PIXEL_WIDTH { get; set; }
[XmlElement]
public int PIXEL_HEIGHT { get; set; }
[XmlElement]
public int PIXEL_DEPTH { get; set; }
[XmlArrayItem(ElementName ="Height")]
[XmlArray]
public List<short> heights { get; set; }
[XmlElement]
public int worldWidthSegments { get; set; }
[XmlElement]
public int worldDepthSegments { get; set; }
#pragma warning restore IDE1006 // Naming Styles
}

这里是产生错误的子类:

public class FlatEnvironment : NdSRD.WebService.Core.DataModel.Environment
{
public readonly static int PIXEL_PER_REAL_METER_RATIO = 100;
public FlatEnvironment(double realWidth, double realDepth, double maxHeight)
{
this.PIXEL_WIDTH = (int)realWidth * PIXEL_PER_REAL_METER_RATIO;
this.PIXEL_DEPTH = (int)realDepth * PIXEL_PER_REAL_METER_RATIO;
this.PIXEL_HEIGHT = (int)maxHeight * PIXEL_PER_REAL_METER_RATIO;
this.worldWidthSegments = 128;
this.worldDepthSegments = 128;
this.id = "FLAT_ENVIRONMENT-WxDxmH:" + realWidth + "x" + realDepth + "x" + maxHeight + "-PWxPDxPH:" + PIXEL_WIDTH + "x" + PIXEL_DEPTH + "x" + PIXEL_HEIGHT + " WSxDS:" + this.worldWidthSegments + "x" + this.worldDepthSegments;
this.realWidth = realWidth;
this.realDepth = realDepth;
this.realHeight = maxHeight;
this.heights = new System.Collections.Generic.List<short>();

for (int i = 0; i < (this.worldDepthSegments + 1) * (this.worldWidthSegments + 1); i++)
{
heights.Add(0);
}

}
}

更新 1:这是我序列化提到的类的方法:

public void Serialize(string fileName, NdSRD.WebService.Core.DataModel.Environment environment)
{
XmlSerializer xs = new XmlSerializer(typeof(NdSRD.WebService.Core.DataModel.Environment));
System.IO.TextWriter writer = new StreamWriter(fileName);
xs.Serialize(writer, environment);
writer.Close();
}

最佳答案

FlatEnvironment 成为子类是否真的有令人信服的理由?看起来它除了生成具有特定值的基类的实例之外并没有真正服务于任何目的,在这种情况下,您最好只拥有一个执行此操作的方法。这将解决您的序列化问题:

// put this in the Environment class or a static class:
public const int PIXEL_PER_REAL_METER_RATIO = 100;
public static Environment FlatEnvironment(double realWidth, double realDepth, double maxHeight)
{
const int worldWidthSegments = 128;
const int worldDepthSegments = 128;
var heights = new System.Collections.Generic.List<short>();

for (int i = 0; i < (worldDepthSegments + 1) * (worldWidthSegments + 1); i++)
{
heights.Add(0);
}
return new Environment {
PIXEL_WIDTH = (int)realWidth * PIXEL_PER_REAL_METER_RATIO,
PIXEL_DEPTH = (int)realDepth * PIXEL_PER_REAL_METER_RATIO,
PIXEL_HEIGHT = (int)maxHeight * PIXEL_PER_REAL_METER_RATIO,
worldWidthSegments = worldWidthSegments,
worldDepthSegments = worldDepthSegments,
id = "FLAT_ENVIRONMENT-WxDxmH:" + realWidth + "x" + realDepth + "x" + maxHeight + "-PWxPDxPH:" + PIXEL_WIDTH + "x" + PIXEL_DEPTH + "x" + PIXEL_HEIGHT + " WSxDS:" + worldWidthSegments + "x" + worldDepthSegments,
realWidth = realWidth,
realDepth = realDepth,
realHeight = maxHeight,
};
}

关于c# - 没有无参数构造函数的子类的 XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com