CsvHelper ConvertUsing不改变输出 [英] CsvHelper ConvertUsing not changing output

查看:354
本文介绍了CsvHelper ConvertUsing不改变输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 ConvertUsing 方法href=\"https://github.com/JoshClose/CsvHelper\" rel=\"nofollow\"> CsvHelper 库(v 2.4.0)。

I'm trying to use the ConvertUsing method of the CsvHelper library (v 2.4.0).

我读过有关的 ConvertUsing ,但不能让它开始工作。

I've read the documentation about ConvertUsing but can't get it to work.

我用一个简单的类:

public class Test
{
    public long Id { get; set; }
    public string Title { get; set; }
}

通过这种 ClassMap

public class TestClassMap : CsvClassMap<Test>
{
    public override void CreateMap()
    {
        Map(m => m.Id).Name("id").ConvertUsing(row => 11111);
        Map(m => m.Title).Name("title").ConvertUsing(row => row.GetField("title") + " 123");
    }
}



我的代码,使用这些创建类的实例然后将它写入到CSV:

My code which uses these creates an instance of the class and then writes it to CSV:

var test = new Test() { Id = 99, Title = "Test title" };

using (var streamWriter = new StreamWriter("test.csv"))
{
    var csv = new CsvWriter(streamWriter);
    csv.Configuration.RegisterClassMap<TestClassMap>();
    csv.WriteRecord(test);
}



不过输出文件 test.csv 始终是以下格式:

id,title
99,Test title

我在寻找的输出是:

id,title
11111,Test title 123

ConvertUsing 被忽略。我试着只转换编号,只有标题,但这并不能工作。

And the ConvertUsing is being ignored. I've tried only converting the Id, and only the Title, but this doesn't work either.

任何想法,我要去哪里错了?

Any ideas where I'm going wrong?

推荐答案

目前 ConvertUsing 阅读时才使用。

您可以使用自定义类型转换器,如果你想自定义输出。您也可以通过类型转换器选项一些有限的能力。

You can use a custom type converter if you want to customize the output. You also have some limited abilities through the type converter options.

这篇关于CsvHelper ConvertUsing不改变输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆