FieldConverter ConverterKind.Date 不支持 "dd/MM/yyyy HH:mm:ss.ffffff"

FieldConverter ConverterKind.Date not supporting "dd/MM/yyyy HH:mm:ss.ffffff"

我正在尝试使用文件帮助程序库读取带分隔符的平面文件。

[FieldConverter(ConverterKind.Date, "yyyy/MM/dd HH:mm:ss.ffffff")]
public DateTime DateOfInteraction;

但我收到以下异常

FileHelpers.ConvertException was unhandled HResult=-2146233088 Message=Error Converting '2015-09-20 23:40:07.707000' to type: 'DateTime'. Using the format: 'yyyy/MM/dd HH:mm:ss.ffffff' Source=FileHelpers ColumnNumber=80 FieldName=DateOfInteraction FieldStringValue=2015-09-20 23:40:07.707000 LineNumber=2 MessageExtra= Using the format: 'yyyy/MM/dd HH:mm:ss.ffffff' MessageOriginal=Error Converting '2015-09-20 23:40:07.707000' to type: 'DateTime'. StackTrace: at FileHelpers.ConvertHelpers.DateTimeConverter.StringToField(String from) at FileHelpers.FieldBase.AssignFromString(ExtractedInfo fieldString, LineInfo line) at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line) at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values) at FileHelpers.FileHelperAsyncEngine1.ReadNextRecord() at FileHelpers.FileHelperAsyncEngine1.ReadNext() at FileHelpers.FileHelperAsyncEngine`1.AsyncEnumerator.MoveNext() at TestApplication.Program.Main(String[] args) in c:\MSRS\Old Sys\Practice\CIMStats\TestApplication\Program.cs:line 22 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

我不明白为什么我会收到异常,即使日期格式字符串是有效的。以下代码片段提供正确的输出...

Console.WriteLine("Started: {0:dd/MM/yyyy HH:mm:ss.ffffff}", DateTime.Now);

自定义日期格式字符串中的“/”是特定于文化的 DateSeparator。所以它被你的文化的日期分隔符所取代。转换时您必须在文化设置中指定 InvariantCulture 而转换