C# FileHelperEngine构造函数-InvalidcastException

C# FileHelperEngine构造函数-InvalidcastException,c#,asp.net,exception,filehelpers,.net,C#,Asp.net,Exception,Filehelpers,.net,我正在尝试使用FileHelpers读取CSV文件(从网站上获得的版本2.0.0,我还尝试了构建2.9.16,但没有成功) 我的CSV课程如下: [DelimitedRecord(";")] public class RawImportCandidate { public string Name1; [FieldNullValue(typeof(int?), null)] public int? ID1; [FieldNullValue(typeof(int?),

我正在尝试使用FileHelpers读取CSV文件(从网站上获得的版本2.0.0,我还尝试了构建2.9.16,但没有成功)

我的CSV课程如下:

[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;
}
(属性名称因不相关而更改)

现在,每当我尝试实例化FileHelperEngine引擎时

FileHelperEngine engine = new FileHelperEngine(typeof(RawImportCandidate));
我得到一个InvalidCastException“Null对象无法转换为值类型”

如果有帮助,下面是异常的stacktrace:

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)

我整个上午都在网上搜索这个问题的答案,但什么也没找到。我真的不知道这是什么原因(这也是我第一次使用FileHelper)。任何帮助都将不胜感激。

正如阿德里亚诺所建议的,使用[FieldNullValue(null)]而不是[FieldNullValue(typeof(int?),null]解决了这个问题。

如果您将[FieldNullValue(typeof(int?),null)]改为简单的[FieldNullValue(null)](或者,更好的是,什么都没有)?谢谢!使用FieldNullvalue(null)实际上修复了它。现在我觉得自己没有尝试这个有点傻,但是filehelpers教程总是使用fieldnullvalue.Sweet库中的类型,顺便说一句@AetherMcLoud@AetherMcLoud大多数情况下,确实需要一些转换的值(例如枚举)都需要它。当可以推断出类型时,它是不必要的(例如,属性可能是System.Object,默认值可能是System.Int32)。对于null(当它是该类型的默认值时),即使省略属性也应该是好的(实际上,我不知道null For nullables是副作用还是预期的行为)。