Search code examples
c#asp.netexceptionfilehelpers.net

FileHelperEngine constructor - InvalidcastException


I'm trying to read a CSV file with FileHelpers (version 2.0.0 from the www.filehelpers.net site, I also tried Build 2.9.16 to no avail).

my CSV class is as follows:

[DelimitedRecord(";")]
public class RawImportCandidate
{
    public string Name1;
    [FieldNullValue(typeof(int?), null)]
    public int? ID1;
    [FieldNullValue(typeof(int?), null)]
    public int? ID2;
    [FieldNullValue(typeof(int?), null)]
    public int? ID3;
    public string Name2;
    public int ID4;
    [FieldConverter(ConverterKind.Date, "dd.MM.yyyy")]
    public DateTime ImportDate;
    public string DtCode;
    [FieldConverter(ConverterKind.Boolean, "1", "0")]
    public bool CheckImport;

    [FieldNullValue(typeof(int?), null)]
    public int? ID5;
    public string Name3;
    [FieldNullValue(typeof(int?), null)]
    public int? ID6;
    public string Name4;
    public string Name5;
    public string Name6;
    public string Name7;
    public string Name8;
    public string Name9;
    public string Name10;
    [FieldNullValue(typeof(int?), null)]
    public int? ID7;
    [FieldNullValue(typeof(int?), null)]
    public int? ID8;
    public string ID9;
    public string ID10;

    [FieldNullValue(typeof(int?), null)]
    public int? ID11;
    public string Name11;
    public string Name12;
    public string Name13;
    public string Name14;
    public string Name15;
    public string Name16;
    public string Name17;
    public string Name18;
    public string Name19;
    public string Name20;

    public string Name21;
    [FieldNullValue(typeof(int?), null)]
    public int? ID12;
    public decimal Weight;
    public int ID13;
    public string Name22;
    public string Name23;
}

(Property names change because they aren't relevant)

Now, whenever I try to instantiate the FileHelperEngine engine like

FileHelperEngine engine = new FileHelperEngine(typeof(RawImportCandidate));

I get a InvalidCastException "Null object cannot be converted to a value type.".

If it helps, here's the stacktrace from the exception:

at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at FileHelpers.FieldNullValueAttribute..ctor(Type type, String nullValue)
   at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
   at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeFieldInfo field, RuntimeType caType)
   at System.Reflection.RuntimeFieldInfo.GetCustomAttributes(Type attributeType, Boolean inherit)
   at FileHelpers.FieldBase..ctor(FieldInfo fi)
   at FileHelpers.DelimitedField..ctor(FieldInfo fi, String sep)
   at FileHelpers.FieldFactory.CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute, Boolean someOptional)
   at FileHelpers.RecordInfo.CreateCoreFields(ArrayList fields, TypedRecordAttribute recordAttribute)
   at FileHelpers.RecordInfo.InitFields()
   at FileHelpers.RecordInfo..ctor(Type recordType)
   at FileHelpers.EngineBase..ctor(Type recordType, Encoding encoding)
   at FileHelpers.FileHelperEngine..ctor(Type recordType, Encoding encoding)
   at FileHelpers.FileHelperEngine..ctor(Type recordType)

I searched the net the whole morning for an answer to the problem, and didn't find anything. I honestly have no idea what is causeing this (this is also my first time using filehelpers). Any help would be appreciated.


Solution

  • As Adriano suggested, using [FieldNullValue(null)] instead of [FieldNullValue(typeof(int?), null)] fixed the problem.