FileHelpers FieldConverter is not called if the value is blank
I am trying to parse a file using FileHelpers. I need to map fields to KeyValuePair and there is a mapping for some of these fields if the line in the file is a space. However, my custom FieldConverter FieldToString method doesn't seem to be called when the line from the file is a space. I want it to be called though!
Here is my field definition:
[FieldFixedLength(1)]
[FieldTrim(TrimMode.Right)]
[FieldConverter(typeof(AreYouOneOfTheFollowingConverter))]
public KeyValuePair<int, string>? AreYouOneOfTheFollowing;
Here is my converter ([case "":] never gets hit):
public class AreYouOneOfTheFollowingConverter : ConverterBase
{
public override object StringToField(string from)
{
switch (from)
{
case "1":
{
return new KeyValuePair<int, string>(1469, "Yes");
}
case " ":
{
return new KeyValuePair<int, string>(1470, "No");
}
default:
{
if (String.IsNullOrWhiteSpace(from))
{
return from;
}
else
{
throw new NotImplementedException();
}
}
}
}
}
Ideas?
+3
source to share
1 answer
ConverterBase
has a virtual method that you can override to control automatic handling of spaces.
public class AreYouOneOfTheFollowingConverter : ConverterBase
{
protected override bool CustomNullHandling
{
/// you need to tell the converter not
/// to handle empty values automatically
get { return true; }
}
public override object StringToField(string from)
{
/// etc...
}
}
+1
source to share