问题
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.
回答1:
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
回答2:
Use the [FieldNullValue("")]
attribute to specify default values for the empty fields.
来源:https://stackoverflow.com/questions/5066255/can-the-filehelpers-library-write-classes-containing-nullable-fields-as-well-as