IWriterConfiguration.ReferenceHeaderPrefix 相当于新版本的 CsvHelper
IWriterConfiguration.ReferenceHeaderPrefix equivalent in newer versions of CsvHelper
新版本的 CsvHelper 中 csvWriter.Configuration.ReferenceHeaderPrefix 的等价物是什么?试试这个
csvWriter.Configuration.ReferenceHeaderPrefix = (memberType, memberName) => $"{memberName}_";
但它不允许我,因为 ReferenceHeaderPrefix 在版本 20.0.0 之后只有 get 方法
通常的工作流程是构造一个实例 CsvConfiguration
class and pass that into the constructor for the reader or writer. And CsvConfiguration.ReferenceHeaderPrefix
确实有一个 set
方法。
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);
}
另请注意,在当前版本 (27.2.0) 中,ReferenceHeaderPrefix
takes a single ReferenceHeaderPrefixArgs
参数包含 MemberType
和 MemberName
字段:
public readonly struct ReferenceHeaderPrefixArgs
{
public readonly Type MemberType;
public readonly string MemberName;
public ReferenceHeaderPrefixArgs(Type memberType, string memberName)
{
MemberType = memberType;
MemberName = memberName;
}
}
新版本的 CsvHelper 中 csvWriter.Configuration.ReferenceHeaderPrefix 的等价物是什么?试试这个
csvWriter.Configuration.ReferenceHeaderPrefix = (memberType, memberName) => $"{memberName}_";
但它不允许我,因为 ReferenceHeaderPrefix 在版本 20.0.0 之后只有 get 方法
通常的工作流程是构造一个实例 CsvConfiguration
class and pass that into the constructor for the reader or writer. And CsvConfiguration.ReferenceHeaderPrefix
确实有一个 set
方法。
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);
}
另请注意,在当前版本 (27.2.0) 中,ReferenceHeaderPrefix
takes a single ReferenceHeaderPrefixArgs
参数包含 MemberType
和 MemberName
字段:
public readonly struct ReferenceHeaderPrefixArgs { public readonly Type MemberType; public readonly string MemberName; public ReferenceHeaderPrefixArgs(Type memberType, string memberName) { MemberType = memberType; MemberName = memberName; } }