3

我正在尝试使用 FileHelpers 读取 CSV 文件(来自www.filehelpers.net站点的版本 2.0.0,我也尝试了 Build 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 对象无法转换为值类型。”。

如果有帮助,这里是异常的堆栈跟踪:

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)

我整个上午都在网上搜索这个问题的答案,但没有找到任何东西。老实说,我不知道是什么原因造成的(这也是我第一次使用文件助手)。任何帮助,将不胜感激。

4

1 回答 1

1

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

于 2013-06-14T20:24:07.390 回答