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

时间:2019-04-14 17:13:32

标签: c#

我正在尝试序列化一个没有其他可序列化类的无参数构造函数的子类。 不管我尝试什么,由于没有无参数构造函数,我总是会收到InvalidOperationException。

我试图通过以下两种方式将子类转换为基类:simlpe转换(通过简单的转换,我指的是介于两者之间的括号,并带有Convert.ChangeType(...)。虽然前一种方法不起作用(我仍然遇到异常),但后者方法是InvalidCastException导致的(消息说该对象必须实现IConvertible接口)。

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

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

1 个答案:

答案 0 :(得分:3)

实际上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, 
    };
}