Csvhelper 将索引动态设置为不同的 csv
Csvhelper set index dynamically to different csv
我从不同的客户那里收到了不同的 CSV,但没有 header(我们就内容达成了一致,但格式没有达成一致),我正在尝试构建一个通用的 class 来解析它的硬耦合它与 CSVHelper(谁知道明天是否不再支持它)。
例如,第一个客户发送 CSV 作为
"Name;Lastname;Age;Birthplace",第二个客户发"Birthplace;Lastname;Firstname;Age",另一个客户发"Firstname;Lastname"。我创建了一个自定义属性,我在其中放置了预期的索引,并且在运行时我想从 CsvHelper 添加 IndexAttribute。
partial class CustomAttribute : Attribute
{
public int Index { get; set; }
}
public class PersonCustomer1 : IPerson
{
[CustomAttribute(Index = 0)]
public string Name { get; set; }
[CustomAttribute(Index = 1)]
public string Lastname { get; set; }
[CustomAttribute(Index = 3)]
public int Age { get; set; }
}
public class PersonCustomer2 : IPerson
{
[CustomAttribute(Index = 2)]
public string Name { get; set; }
[CustomAttribute(Index = 1)]
public string Lastname { get; set; }
[CustomAttribute(Index = 0)]
public int Age { get; set; }
[CustomAttribute(Index = 3)]
public int Gender { get; set; }
}
通用转换器构建为
public class ConverterCsv<T>
{
public IEnumerable<T> Convert(FileInfo fileInfo)
{
IEnumerable<T> records = new List<T>();
using (var reader = fileInfo.OpenText())
using (var csv = new CsvReader(reader))
{
csv.Configuration.HasHeaderRecord = false;
csv.Configuration.RegisterClassMap<CsvGenericMap<T>>();
records = csv.GetRecords<T>().ToList();
}
return records;
}
}
public sealed class CsvGenericMap<T> : ClassMap<T>
{
public CsvGenericMap()
{
foreach (var property in typeof(T).GetProperties())
{
var attribute = property?.GetCustomAttribute<CustomAttribute>();
//what now? How should I inject the CsvHelper.Configuration.Attributes.Index attribute?
}
}
}
我希望任何设置了自定义属性的 csv 都将填充 class。
public sealed class CsvGenericMap<T> : ClassMap<T>
{
public CsvGenericMap()
{
foreach (var property in typeof(T).GetProperties())
{
var attribute = property?.GetCustomAttribute<CustomAttribute>();
Map(typeof(T), property).Index(attribute.Index);
}
}
}
我从不同的客户那里收到了不同的 CSV,但没有 header(我们就内容达成了一致,但格式没有达成一致),我正在尝试构建一个通用的 class 来解析它的硬耦合它与 CSVHelper(谁知道明天是否不再支持它)。 例如,第一个客户发送 CSV 作为 "Name;Lastname;Age;Birthplace",第二个客户发"Birthplace;Lastname;Firstname;Age",另一个客户发"Firstname;Lastname"。我创建了一个自定义属性,我在其中放置了预期的索引,并且在运行时我想从 CsvHelper 添加 IndexAttribute。
partial class CustomAttribute : Attribute
{
public int Index { get; set; }
}
public class PersonCustomer1 : IPerson
{
[CustomAttribute(Index = 0)]
public string Name { get; set; }
[CustomAttribute(Index = 1)]
public string Lastname { get; set; }
[CustomAttribute(Index = 3)]
public int Age { get; set; }
}
public class PersonCustomer2 : IPerson
{
[CustomAttribute(Index = 2)]
public string Name { get; set; }
[CustomAttribute(Index = 1)]
public string Lastname { get; set; }
[CustomAttribute(Index = 0)]
public int Age { get; set; }
[CustomAttribute(Index = 3)]
public int Gender { get; set; }
}
通用转换器构建为
public class ConverterCsv<T>
{
public IEnumerable<T> Convert(FileInfo fileInfo)
{
IEnumerable<T> records = new List<T>();
using (var reader = fileInfo.OpenText())
using (var csv = new CsvReader(reader))
{
csv.Configuration.HasHeaderRecord = false;
csv.Configuration.RegisterClassMap<CsvGenericMap<T>>();
records = csv.GetRecords<T>().ToList();
}
return records;
}
}
public sealed class CsvGenericMap<T> : ClassMap<T>
{
public CsvGenericMap()
{
foreach (var property in typeof(T).GetProperties())
{
var attribute = property?.GetCustomAttribute<CustomAttribute>();
//what now? How should I inject the CsvHelper.Configuration.Attributes.Index attribute?
}
}
}
我希望任何设置了自定义属性的 csv 都将填充 class。
public sealed class CsvGenericMap<T> : ClassMap<T>
{
public CsvGenericMap()
{
foreach (var property in typeof(T).GetProperties())
{
var attribute = property?.GetCustomAttribute<CustomAttribute>();
Map(typeof(T), property).Index(attribute.Index);
}
}
}