FileHelpers库-是否可以在运行时更改字段的日期格式?

FileHelpers library - Is it possible to change field's date format at runtime?
2020-10-18
  •  译文(汉语)
  •  原文(英语)

使用FileHelpers库版本3.0.1.

假设您定义了一个要从csv文件导入的字段,如下所示:

    [FieldTrim(TrimMode.Both)] 
    [FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
    [FieldConverter(ConverterKind.Date, "d-MMMM-yyyy")]
    public DateTime DOB;

现在,csv字段应采用日期格式"d-MMMM-yyyy",并在编译时设置.我在运行时有要求(有时)将此格式更改为"d-MMMM-yy".

使用的格式取决于源文件-有些文件有2位数字的年份,另一些文件有4位数字的年份.我将在程序启动时知道使用哪种格式.

FileHelpers有可能吗?我想我可以添加一个字符串列,如:

[FieldTrim(TrimMode.Both)] 
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public string DOBAsString; 

,并让FileHelpers将该数据读取到该字段中,然后自己循环将其转换为DOB字段.有没有更清洁的方法?

速聊1:
您可以在运行时更改FieldConverter属性的值.您可以在此处查看示例 codinglight.blogspot.in/2008/10/…
速聊2:
谢谢,但是我不确定这是否可以与FileHelpers一起使用,因为FileHelpers负责创建字段所在的对象的实例.因此,似乎没有钩子可以获取对象的实例(?).
速聊3:
我明白.如果找不到使用FileHelpers解决此问题的方法,则仍可以在运行时更改属性(即使FileHelpers创建了实例).您可以将这种解决方案作为最后的选择.
解决过程1

最简单的方法是使用ConverterKind.DateMultiFormat而不是ConverterKind.Date

[FieldTrim(TrimMode.Both)] 
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
[FieldConverter(ConverterKind.DateMultiFormat, "d-MMMM-yyyy", "d-MMMM-yy"]
public DateTime DOB;

通常,在运行时指定格式的另一种方法是将运行时类与ClassBuilder一起使用:

FixedClassBuilder cb = new FixedLengthClassBuilder("Customers"); 

cb.AddField("DOB", 8, typeof(DateTime)); 
cb.LastField.Converter.Kind = ConverterKind.Date; 
cb.LastField.Converter.Arg1 = "d-MMMM-yyyy"; // or whatever
cb.LastField.TrimMode = TrimMode.Both; 
cb.LastField.FieldNullValue = DateTime.Now;          

engine = new FileHelperEngine(cb.CreateRecordClass()); 

Using FileHelpers library version 3.0.1 .

Say you define a field to import from a csv file like this :

    [FieldTrim(TrimMode.Both)] 
    [FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
    [FieldConverter(ConverterKind.Date, "d-MMMM-yyyy")]
    public DateTime DOB;

Now, the csv field is expected to be in the date format "d-MMMM-yyyy", and set at compile time. I have a requirement at runtime to (sometimes) change this format to "d-MMMM-yy".

The format to use depends on the source file - some files have 2 digit years, others have 4 digit years. I will know at program startup which format to use.

Is this possible with FileHelpers ? I suppose I could add a string column like :

[FieldTrim(TrimMode.Both)] 
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
public string DOBAsString; 

, and get FileHelpers to read the data into this field, and convert it to the DOB field myself in a loop. Is there a cleaner way ?

Talk1:
you can change the value of the FieldConverter attribute at runtime. You can see an example here codinglight.blogspot.in/2008/10/…
Talk2:
thanks, but I'm not sure if this will work with FileHelpers, because FileHelpers is in charge of creating the instances of the objects in which the field resides. So there seems to be no hook to be able to get an instance of the object (?).
Talk3:
I understand. If you cannot find a way to solve this issue with FileHelpers, you can still change the attribute at runtime (even if FileHelpers created the instance). You can have this solution as a last resort.
Solutions1

The easiest way would be to use the ConverterKind.DateMultiFormat instead of ConverterKind.Date

[FieldTrim(TrimMode.Both)] 
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow)]
[FieldConverter(ConverterKind.DateMultiFormat, "d-MMMM-yyyy", "d-MMMM-yy"]
public DateTime DOB;

In general, another way of specifying the format at runtime is to use the runtime classes with a ClassBuilder:

FixedClassBuilder cb = new FixedLengthClassBuilder("Customers"); 

cb.AddField("DOB", 8, typeof(DateTime)); 
cb.LastField.Converter.Kind = ConverterKind.Date; 
cb.LastField.Converter.Arg1 = "d-MMMM-yyyy"; // or whatever
cb.LastField.TrimMode = TrimMode.Both; 
cb.LastField.FieldNullValue = DateTime.Now;          

engine = new FileHelperEngine(cb.CreateRecordClass()); 
转载于:https://stackoverflow.com/questions/30773888/filehelpers-library-is-it-possible-to-change-fields-date-format-at-runtime

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入