FileHelpers 库可以编写包含可为空字段的类并读取它们吗?
我正在使用 FileHelpers 库的 2.0 版本,该库被记录为能够处理 .NET 2.0 Nullable 类型。
我正在使用文档中示例中给出的代码:
[DelimitedRecord("|")]
public class Orders
{
public int OrderID;
public DateTime? OrderDate;
[FieldConverter(ConverterKind.Date, "ddMMyyyy")]
public DateTime? RequiredDate;
public int? ShipVia;
}
使用 FileHelperEngine,我可以成功读取一个没有 OrderDate、RequiredDate 或 ShipVia 字段值的文件。该文件如下所示:
1|||
但是,我无法将生成的 Orders[] 写入文件 - 库抛出 NullReferenceException,堆栈跟踪如下:
at FileHelpers.ConvertHelpers.CultureConverter.FieldToString(Object from) 在 FileHelpers.FieldBase.BaseFieldString(Object fieldValue) 在 FileHelpers.DelimitedField.CreateFieldString(StringBuilder sb,对象 fieldValue) 在 FileHelpers.FieldBase.AssignToString(StringBuilder sb,对象 fieldValue) 在 FileHelpers.RecordInfo.RecordToString(对象记录) 在 FileHelpers.FileHelperEngine1.WriteStream(TextWriter writer, IEnumerable
1 条记录,Int32 maxRecords) 在 FileHelpers.FileHelperEngine1.WriteFile(String fileName, IEnumerable
1 条记录, Int32 maxRecords) 在 FileHelpers.FileHelperEngine1.WriteFile(String fileName, IEnumerable
1 条记录) 在C:\dev\src\TestingFileHelpers\TestingFileHelpers\Program.cs中的TestingFileHelpers.Program.Main(String[] args):第19行 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,字符串 [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()
我确信我一定在这里遗漏了一些东西,但我不知道它是什么。非常感谢任何帮助。
I'm using version 2.0 of the FileHelpers library which is documented as being able to handle .NET 2.0 Nullable types.
I'm using the code given in the example from the documentation:
[DelimitedRecord("|")]
public class Orders
{
public int OrderID;
public DateTime? OrderDate;
[FieldConverter(ConverterKind.Date, "ddMMyyyy")]
public DateTime? RequiredDate;
public int? ShipVia;
}
With a FileHelperEngine I can successfully read in a file which has no value for the OrderDate, RequiredDate or ShipVia fields. The file looks like:
1|||
However, I cannot then write out the resulting Orders[] to file - the library throws a NullReferenceException, stack trace below:
at FileHelpers.ConvertHelpers.CultureConverter.FieldToString(Object from)
at FileHelpers.FieldBase.BaseFieldString(Object fieldValue)
at FileHelpers.DelimitedField.CreateFieldString(StringBuilder sb, Object fieldValue)
at FileHelpers.FieldBase.AssignToString(StringBuilder sb, Object fieldValue)
at FileHelpers.RecordInfo.RecordToString(Object record)
at FileHelpers.FileHelperEngine1.WriteStream(TextWriter writer, IEnumerable
1 records, Int32 maxRecords)
at FileHelpers.FileHelperEngine1.WriteFile(String fileName, IEnumerable
1 records, Int32 maxRecords)
at FileHelpers.FileHelperEngine1.WriteFile(String fileName, IEnumerable
1 records)
at TestingFileHelpers.Program.Main(String[] args) in C:\dev\src\TestingFileHelpers\TestingFileHelpers\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I'm sure I must be missing something here but I cannot figure out what it is. Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用该库的最新版本:
http: //teamcity.codebetter.com/viewLog.html?buildId=21768&tab=artifacts&buildTypeId=bt65(以访客身份登录)
如果该版本修复了错误,请在评论中告诉我,我将添加测试用例到库以确保其正常工作
Can you try with the last version of the library:
http://teamcity.codebetter.com/viewLog.html?buildId=21768&tab=artifacts&buildTypeId=bt65 (login as guest)
If that version dot fix the error just tell me in a comment and I will add a test case to the lib to ensure it works
使用
[FieldNullValue("")]
属性指定空字段的默认值。Use the
[FieldNullValue("")]
attribute to specify default values for the empty fields.