我正在尝试使用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)
我整个上午都在网上搜索问题的答案,但没有找到任何答案。老实说,我不知道是什么导致这个(这也是我第一次使用filehelpers)。任何帮助将不胜感激。
答案 0 :(得分:1)
正如Adriano建议的那样,使用[FieldNullValue(null)]而不是[FieldNullValue(typeof(int?),null)]修复了问题。