FileHelpers cannot map converted field into destination array

Posted by jaffa on Stack Overflow See other posts from Stack Overflow or by jaffa
Published on 2012-06-19T09:12:55Z Indexed on 2012/06/19 9:16 UTC
Read the original article Hit count: 209

Filed under:
|
|
|
|

I have the following record (reduced for brevity):

 [DelimitedRecord(",")]
    [IgnoreFirst]
    [IgnoreEmptyLines()]
    public class ImportRecord
    {
        [FieldQuoted]
        [FieldTrim(TrimMode.Both)]
        public string FirstName;

        [FieldQuoted]
        [FieldTrim(TrimMode.Both)]
        public string LastName;

        [FieldQuoted]
        [FieldTrim(TrimMode.Both)]
        [FieldOptional]
        [FieldConverter(typeof(TestPropertyConverter))]
        public int[] TestProperty;    
    }

Converter code:

  public class TestPropertyConverter : ConverterBase
    {
        public override object StringToField(string from)
        {
              var ret = from.Split('|').Select(x => Convert.ToInt32(x)).ToArray();
              return ret;
        }
    }

So an example record could be: John, Smith, 1|2|3|4

It would expect the values 1,2,3,4 to expand and fill the TestProperty array. However, I'm getting the following exception:

At least one element in the source array could not be cast down to the destination array type.

I've tried to debug into the code and it seems to blow-up in the ExtractFieldValue() function inside FieldBase.cs where it tries to return out of the function. The following line seems to be the culprit:

res.ToArray(ArrayType);

It seems to expect the 'res' variable to be the destination type array, but it contains 1 element of the array itself.

Can anyone suggest if I'm doing this wrong or a possible fix?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET