c# - FileHelpers 中的多个日期如何?

标签 c# filehelpers

我想知道如何在 filehelpers 中执行多个日期?似乎您必须指定要支持的每一种格式(有点糟糕......希望它可以处理更多基本格式)。

   [FieldOrder(1), FieldConverter(ConverterKind.DateMultiFormat, "MM/dd/yyyy", "MM/d/yyyy", "M/d/yyyy","M/dd/yyyy")]

虽然这给了我

Error   35  Argument 1: cannot convert from 'FileHelpers.ConverterKind' to 'System.Type'    

看来我必须进行一些自定义转换或其他操作?这是正确的吗?

编辑

我使用的版本是 2.9.9.0

选项

// Summary:
//     Indicates the FileHelpers.ConverterKind used for read/write operations.
//
// Remarks:
//     See the Complete attributes list for more information and examples of each
//     one.
[AttributeUsage(AttributeTargets.Field)]
public sealed class FieldConverterAttribute : Attribute
{
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    public FieldConverterAttribute(ConverterKind converter);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    public FieldConverterAttribute(Type customConverter);
    //
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    public FieldConverterAttribute(ConverterKind converter, string arg1);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   args:
    //     A list of params passed directly to your converter constructor.
    public FieldConverterAttribute(Type customConverter, params object[] args);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    public FieldConverterAttribute(Type customConverter, string arg1);
    //
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    public FieldConverterAttribute(Type customConverter, string arg1, string arg2);
    //
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    //
    //   arg3:
    //     The third param passed directly to the Converter Constructor.
    public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2, string arg3);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    //
    //   arg3:
    //     The third param passed directly to the Converter Constructor.
    public FieldConverterAttribute(Type customConverter, string arg1, string arg2, string arg3);
}

最佳答案

没有带参数的重载 FieldConverterAttribute(ConverterKind, params string[])

有一个 FieldConverterAttribute(ConverterKind, string, string, string) 这样您最多可以提供 3 种格式。

如果您需要更多,那么您可以创建自己的转换器:

public class CustomDateTimeConverter : ConverterBase
{
    public CustomDateTimeConverter(string format1, string format2, string format3, string format4)
    {
        _FormatStrings = new string[] { format1, format2, format3, format4} ;
    }

    private string[] _FormatStrings;

    public override object StringToField(string from)
    {
        foreach (string formatString in _FormatStrings)
        {
            DateTime dt;
            if (DateTime.TryParseExact(from, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                return dt;
        }
        throw new ConvertException(from, typeof(DateTime));
    }
}

然后你的字段看起来像

[FieldConverter(typeof(CustomDateTimeConverter), "MM/dd/yyyy", "MM/d/yyyy", "M/d/yyyy", "M/dd/yyyy")]
public DateTime Field1;

关于c# - FileHelpers 中的多个日期如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9133011/

相关文章:

c# - 如果表存在,则返回 bool 值的 SQL Server 存储过程,c# 实现

c# - 以编程方式在 LinqPad 中执行类似 `Hyperlinq` 的查询

c# - FileHelpers 字段中的引号和逗号

c# - Use Reflection to Build a Class(构建一个动态的FileHelper类)

c# - 将实际记录作为属性包含在 Filehelpers 记录对象中

c# - 如何替换前导空格但保留多行?

C# linq to sql - 动态选择表

c# - .NET 如何从匿名类型的枚举中输出 csv?

c# - 使用 Filehelpers 处理 DelimitedRecord 中的换行符

c# - asp.net-mvc 中的鼠标监听器