从类映射中写出自定义标头

时间:2018-07-03 10:25:34

标签: c# .net streamwriter csvhelper

我有一个需要这样写的文件(该文件在单独的行上包含更多字段)

"message": "This website is using browser cookies as per our internal cookie 
policy and privacy policy. By entering our website, you acknowledge that you 
have read and understood our privacy policy", 

我开始使用CsvHelper库。

到目前为止,我有以下课程可以写出文件

|USERID.   |12345
|USERNAME.   |John Smit

我也有一个映射类

var testRecords = new List<MembershipDetail>();
testRecords.Add(new MembershipDetail() { USER_ID = "12345" });

using (TextWriter writer = new StreamWriter(@"C:\test.csv"))
{
    var csv = new CsvWriter(writer);
    csv.Configuration.Delimiter = "   |a";
    csv.Configuration.RegisterClassMap<FieldMapping>();

    foreach(var testRecord in testRecords)
    {
         csv.WriteField(testRecord.USER_ID);
         csv.NextRecord();
    }                
}

我想从字段映射类中写出Name,这可能吗?目前,我可以通过将字段写为字符串来解决此问题,但我想知道是否有更好的方法。

这是我可以通过字符串执行的操作(但要避免)

public class FieldMapping : ClassMap<MembershipDetail>
{
    public FieldMapping()
    {
        Map(m => m.USER_ID).Name("|USER_ID.");
    }
}

0 个答案:

没有答案