避免XML可序列化类中的重复代码

本文关键字:代码 XML 序列化 避免 | 更新日期: 2023-09-27 18:21:00

我有两个代码非常相似的Serializable类。实际上,除了调用特定构造函数的部分外,序列化代码是相同的。

有没有一种方法可以创建一个包含公共部分的公共类,以便特定的类(子类?)只能实现构造函数部分?我能想到仿制药,工厂模式,但不知道怎么做

// Fictitious classes
[Serializable]
public class FlightParameters {
    public double MaxHeight { get; set; }
    pulbic double MaxSpeedKmPerHour { get; set; }
    public static FlightParameters Load(String fname) {
        FlightParameters result;
        using (var fs = new FileStream(fname, FileMode.OpenOrCreate)) {
            var serializer = new XmlSerializer(typeof(FlightParameters));
            try {
                result = (FlightParameters)serializer.Deserialize(fs);
            }
            // catch "file not found"
            catch (InvalidOperationException) {
                result = new FlightParameters() {
                    MaxHeight = 30000;
                    MaxSpeedKmPerHour = 1500;
                }
                serializer.Serialize(fs, result);
            }
            return result;
        }
    }
}

[Serializable]
public class SailingParameters {
    public double MaxDepth { get; set; }
    pulbic double MaxSpeedKnots { get; set; }
    public static SailingParameters Load(String fname) {
        SailingParameters result;
        using (var fs = new FileStream(fname, FileMode.OpenOrCreate)) {
            var serializer = new XmlSerializer(typeof(SailingParameters));
            try {
                result = (SailingParameters)serializer.Deserialize(fs);
            }
            // catch "file not found"
            catch (InvalidOperationException) {
                result = new SailingParameters() {
                    MaxDepth = 13000;
                    MaxSpeedKnots = 15;
                }
                serializer.Serialize(fs, result);
            }
            return result;
        }
    }
}

用法:

FlightParameters _fparam = FlightParameters.Load(somePath);
SailingParameters _sparam = SailingParameters.Load(someOtherPath);

避免XML可序列化类中的重复代码

我能看到的最简单的方法是:

static class XmlUtils {
    public static T Load<T>(string filename, Func<T> onMissing = null)
        where T : class, new()
    {
        using (var fs = File.OpenRead(filename)) {
            var serializer = new XmlSerializer(typeof(T));
            try {
                return (T)serializer.Deserialize(fs);
            } catch (InvalidOperationException) { // catch "file not found"
                return onMissing == null ? new T() : onMissing();
            }
        }
    }
}

允许类似的事情;

public static SailingParameters Load(string filename) {
    return XmlUtils.Load<SailingParameters>(filename, () => new SailingParameters {
        MaxDepth = 13000;
        MaxSpeedKnots = 15;
    });
}