Home > Net >  IWriterConfiguration.ReferenceHeaderPrefix equivalent in newer versions of CsvHelper
IWriterConfiguration.ReferenceHeaderPrefix equivalent in newer versions of CsvHelper

Time:05-20

What is the equivalent of csvWriter.Configuration.ReferenceHeaderPrefix in the newer version of CsvHelper? Trying this

csvWriter.Configuration.ReferenceHeaderPrefix = (memberType, memberName) => $"{memberName}_";

but its not let me because ReferenceHeaderPrefix has only get method after version 20.0.0

CodePudding user response:

The usual workflow is to construct an instance of the CsvConfiguration class and pass that into the constructor for the reader or writer. And CsvConfiguration.ReferenceHeaderPrefix does have a set method.

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
    ReferenceHeaderPrefix = (args) => $"{args.MemberName}_",
};
using (var writer = new StreamWriter("path\\to\\file.csv"))
using (var csv = new CsvWriter(writer, config))
{
    // Write your CSV records here.
    csv.WriteRecords(records);
}   

Note also that, in the current version (27.2.0), the ReferenceHeaderPrefix takes a single ReferenceHeaderPrefixArgs argument, which contains MemberType and MemberName fields:

public readonly struct ReferenceHeaderPrefixArgs
{
  public readonly Type MemberType;

  public readonly string MemberName;

  public ReferenceHeaderPrefixArgs(Type memberType, string memberName)
  {
      MemberType = memberType;
      MemberName = memberName;
  }
}
  • Related