Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IWriterConfiguration.ReferenceHeaderPrefix equivalent in newer versions of CsvHelper

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading