Open nfplee opened 1 year ago
Just a follow up that I had to modify my converter to feed in the value types. For example:
public class MyIDictionaryGenericConverter : IDictionaryConverter
{
private readonly Type[] _valueTypes;
public MyIDictionaryGenericConverter(Type[] valueTypes)
{
_valueTypes = valueTypes;
}
public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
{
var dictionary = new Dictionary<string, object?>();
var indexEnd = memberMapData.IndexEnd < memberMapData.Index
? row.Parser.Count - 1
: memberMapData.IndexEnd;
var counter = 0;
for (var i = memberMapData.Index; i <= indexEnd; i++)
{
var value = row.GetField(i);
var converter = row.Context.TypeConverterCache.GetConverter(_valueTypes[counter]);
var field = !string.IsNullOrEmpty(value) ? converter.ConvertFromString(value, row, memberMapData) : null;
dictionary.Add(row.HeaderRecord[i], field);
counter++;
}
return dictionary;
}
}
Say I have the following model and mapping:
Now if I say:
I receive the error:
I added the following dictionary converter to fix my issue:
But it would be nice if this was supported out of the box. Also it would be much nicer if there was a better way of adding a header for all the keys in the dictionary. The only way I have managed to achieve it, is to write it manually (as shown above).