CsvHelper 为列定义自定义映射
CsvHelper define custom mapping for a column
我正在尝试使用 CSVHelper 填充 CSV 文件中未提供的列。
这是我需要导入的 CSV 示例
Id
10
123
45
213
我要反序列化的class是这个:
public class Foo {
public int Id { get; set }
public string Name { get; set }
}
使用默认配置我得到这个错误:
CsvHelper.HeaderValidationException: 'Header with name 'Name' was not found.
我希望有可能定义一个映射器,以便解析器可以填充列 Name
(例如,通过提供值字典)。有什么办法吗?
谢谢
------------ 编辑
澄清一下,我们的想法是拥有一个类似转换器的东西,关联到一个字段,用于将 Id 解码为 Name
public class NameConverter
{
public NameConverter()
{
employeesList = new Dictionary<int, string>()
{
{ 10, "Mary" },
{ 45, "Mike" },
{ 123, "Jack" },
{ 213, "Suzanne" },
};
}
IDictionary<int, string> employeesList;
public string GetValue(int id) => employeesList[id];
}
我想,另一种方法是按照建议忽略 Name 字段,并在 Foo class 中注入 NameConverter 并使 Name 成为仅获取 属性。
void Main()
{
var s = new StringBuilder();
s.AppendLine("Id");
s.AppendLine("45");
s.AppendLine("123");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap<FooMap>();
csv.GetRecords<Foo>().ToList().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
var nameConverter = new NameConverter();
Map(m => m.Id);
Map(m => m.Name).ConvertUsing(row => nameConverter.GetValue(row.GetField<int>(nameof(Foo.Id))));
}
}
public class NameConverter
{
public NameConverter()
{
employeesList = new Dictionary<int, string>()
{
{ 10, "Mary" },
{ 45, "Mike" },
{ 123, "Jack" },
{ 213, "Suzanne" },
};
}
IDictionary<int, string> employeesList;
public string GetValue(int id) => employeesList[id];
}
输出:
我正在尝试使用 CSVHelper 填充 CSV 文件中未提供的列。 这是我需要导入的 CSV 示例
Id
10
123
45
213
我要反序列化的class是这个:
public class Foo {
public int Id { get; set }
public string Name { get; set }
}
使用默认配置我得到这个错误:
CsvHelper.HeaderValidationException: 'Header with name 'Name' was not found.
我希望有可能定义一个映射器,以便解析器可以填充列 Name
(例如,通过提供值字典)。有什么办法吗?
谢谢
------------ 编辑
澄清一下,我们的想法是拥有一个类似转换器的东西,关联到一个字段,用于将 Id 解码为 Name
public class NameConverter
{
public NameConverter()
{
employeesList = new Dictionary<int, string>()
{
{ 10, "Mary" },
{ 45, "Mike" },
{ 123, "Jack" },
{ 213, "Suzanne" },
};
}
IDictionary<int, string> employeesList;
public string GetValue(int id) => employeesList[id];
}
我想,另一种方法是按照建议忽略 Name 字段,并在 Foo class 中注入 NameConverter 并使 Name 成为仅获取 属性。
void Main()
{
var s = new StringBuilder();
s.AppendLine("Id");
s.AppendLine("45");
s.AppendLine("123");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap<FooMap>();
csv.GetRecords<Foo>().ToList().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
var nameConverter = new NameConverter();
Map(m => m.Id);
Map(m => m.Name).ConvertUsing(row => nameConverter.GetValue(row.GetField<int>(nameof(Foo.Id))));
}
}
public class NameConverter
{
public NameConverter()
{
employeesList = new Dictionary<int, string>()
{
{ 10, "Mary" },
{ 45, "Mike" },
{ 123, "Jack" },
{ 213, "Suzanne" },
};
}
IDictionary<int, string> employeesList;
public string GetValue(int id) => employeesList[id];
}
输出: