CSV辅助人员中的列的完全合格的名称(避免重复列名称并确保掌握权)

发布于 2025-01-30 06:53:04 字数 1056 浏览 3 评论 0原文

我要求CSV的标头代表对象上属性的完全资格的名称 /路径,以确保转换为CSV时的势力。

一个示例:如果对象具有相同类型的多个属性,则我希望完全符合名称。 Consider the following structure:

public class Foo
{
    public DateTime? MyDateTime { get; set; }
    public string? MyDescription { get; set; }
}

public class MyContainer
{
    public Foo? MyFirstUsage { get; set; }
    public Foo? MySecondUsage { get; set; }
}

Under normal circumstances csv helper would create the header for 'MyContainer' as "MyDateTime,MyDescription,MyDateTime,MyDescription"

I'd like to avoid this and qualify the names resulting in a header of "MyFirstUsageMyDateTime,MyFirstUsageMyDescription,MySecondUsageMyDateTime ,mysecondusagemydescription“

tl; dr:如何覆盖csvhelper扁平的对象写入标头,从而使标头代表属性的完全合格的路径,从而(理论上)确保了势力?

编辑:我创建了一个小型示例应用程序,希望更好地解释问题: https://github.com /jimhume/csvhelpercolumnnames/tree/main

edit2:我忘了提到一个要求是为了通过映射而不是通过映射完成 - 需要动态地完成。

I require the header of a CSV to represent the fully qualified name / path of properties on an object to ensure idempotency when converting to csv.

An example: if an object has multiple properties of the same type I would like to fully qualify the name. Consider the following structure:

public class Foo
{
    public DateTime? MyDateTime { get; set; }
    public string? MyDescription { get; set; }
}

public class MyContainer
{
    public Foo? MyFirstUsage { get; set; }
    public Foo? MySecondUsage { get; set; }
}

Under normal circumstances csv helper would create the header for 'MyContainer' as "MyDateTime,MyDescription,MyDateTime,MyDescription"

I'd like to avoid this and qualify the names resulting in a header of "MyFirstUsageMyDateTime,MyFirstUsageMyDescription,MySecondUsageMyDateTime,MySecondUsageMyDescription"

TL;DR: how can one override the way CSVHelper flattens objects to write the header so that the header represents the fully qualified path of a property, thus (theoretically) ensuring idempotency?

Edit: I created a small sample app to hopefully better explain the issue: https://github.com/JimHume/CsvHelperColumnNames/tree/main

Edit2: I forgot to mention that one requirement is for this to not be done via mapping--it needs to be done dynamically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热血少△年 2025-02-06 06:53:04

Update :发现您可以在配置中使用Reference Headerprefix

void Main()
{
    var mySample = new MyContainer
    {
        MyFirstUsage = new Foo
        {
            MyDateTime = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1)),
            MyDescription = "This is the first usage"
        },
        MySecondUsage = new Foo
        {
            MyDateTime = DateTime.UtcNow.AddDays(1),
            MyDescription = "This is the second usage"
        }
    };

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        ReferenceHeaderPrefix = (args) => $"{args.MemberName}",
    };

    using (var csvWriter = new CsvWriter(Console.Out, config))
    {
        csvWriter.WriteRecords(new[] { mySample });
    }
}

// You can define other methods, fields, classes and namespaces here
public class Foo
{
    public DateTime? MyDateTime { get; set; }
    public string MyDescription { get; set; }
}

public class MyContainer
{
    public Foo MyFirstUsage { get; set; }
    public Foo MySecondUsage { get; set; }
}

Update: Discovered you can use ReferenceHeaderPrefix in the configuration.

void Main()
{
    var mySample = new MyContainer
    {
        MyFirstUsage = new Foo
        {
            MyDateTime = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1)),
            MyDescription = "This is the first usage"
        },
        MySecondUsage = new Foo
        {
            MyDateTime = DateTime.UtcNow.AddDays(1),
            MyDescription = "This is the second usage"
        }
    };

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        ReferenceHeaderPrefix = (args) => 
quot;{args.MemberName}",
    };

    using (var csvWriter = new CsvWriter(Console.Out, config))
    {
        csvWriter.WriteRecords(new[] { mySample });
    }
}

// You can define other methods, fields, classes and namespaces here
public class Foo
{
    public DateTime? MyDateTime { get; set; }
    public string MyDescription { get; set; }
}

public class MyContainer
{
    public Foo MyFirstUsage { get; set; }
    public Foo MySecondUsage { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文