使用 CsvHelper 写入 CSV 文件时的列排序顺序

Column sortorder when writing CSV file using CsvHelper

当使用 CsvHelper 从 .Net objects 文件编写 TSV 时,我想使用属性控制列排序顺序。 使用 CsvHelper-provided Index(..) 属性时,我可以控制序列化顺序,但列 header 后缀为索引:

型号,继承:

    /// <summary>
    /// Generic daily data
    /// </summary>
    public class DailyData
    {
        /// <summary>
        /// 
        /// </summary>
        [CsvHelper.Configuration.Attributes.Index(-2, -2)]
        public int IdDay {get;set;}
    }

    /// <summary>
    /// Generic hou-on-a-day data
    /// </summary>
    public class HourlyData : DailyData
    {
        /// <summary>
        /// 
        /// </summary>
        [CsvHelper.Configuration.Attributes.Index(-1)]
        public byte IdHour { get; set; }
    }

    /// <summary>
    /// 
    /// </summary>
    public class HourlyWeatherInfo : HourlyData
    {
        /// <summary>
        /// 
        /// </summary>
        public double Temperature { get; internal set; }
    }

输出:

IdDay1  IdHour1 Temperature
20220516    18  291.7
20220516    21  289.55
20220517    0   287.3
20220517    3   286.33

我对索引使用负数,因为我希望基数 class 的属性首先列出,而不必在派生的 classes 中处理序列化顺序(如 HourlyWeatherInfo.

我是否忽略了功能?

感觉有点hacky,但这似乎有效。

    /// <summary>
    /// Generic daily data
    /// </summary>
    public class DailyData
    {
        /// <summary>
        /// 
        /// </summary>
        [CsvHelper.Configuration.Attributes.Index(-2,-3)]
        public int IdDay {get;set;}
    }

    /// <summary>
    /// Generic hou-on-a-day data
    /// </summary>
    public class HourlyData : DailyData
    {
        /// <summary>
        /// 
        /// </summary>
        [CsvHelper.Configuration.Attributes.Index(-1,-2)]
        public byte IdHour { get; set; }
    }

    /// <summary>
    /// 
    /// </summary>
    public class HourlyWeatherInfo : HourlyData
    {
        /// <summary>
        /// 
        /// </summary>
        public double Temperature { get; internal set; }
    }