5

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);
}
4

2 に答える 2

11

parameters のオーバーロードはありませんFieldConverterAttribute(ConverterKind, params string[])

1つ付いて 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;
于 2012-02-03T18:47:04.360 に答える
1

最初のコメントについては、Filehelpers については知りませんが、私が使用したすべてのライブラリでは、認識/解析する形式を指定する必要があります。.Net の DateTime 機能も同じです。ただし、.Net には、すべての組み込み形式を返すことができる便利な関数が用意されており、TryParse()( documentation ) で反復処理できます。

たとえば、次のコードは組み込みDateTimeFormatInfo.GetAllDateTimeFormats()(ドキュメントはこちら) を使用して、128 個のカスタム日時形式をループします。(このコードは「ラウンドトリップ」を示しています: 日付を文字列に変換してから文字列を解析します):

using System;
using System.Globalization;
public class Example
{
      public static void Main()
   {
      DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;

      char[] formats = { 'd', 'D', 'f', 'F', 'g', 'G', 'm', 'o', 
                           'r', 's', 't', 'T', 'u', 'U', 'y' };
      DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
      DateTime date2;

      foreach (var fmt in formats) 
      {
         foreach (var pattern in myDTFI.GetAllDateTimePatterns(fmt))
         {
            // "round-trip" = convert the date to string, then parse it
            if (DateTime.TryParse(date1.ToString(pattern), out date2))
            {
                Console.WriteLine("{0:" + pattern + "} (format '{1}')",
                                  date1, pattern);
            }
         }
      }
   }
}
于 2012-02-03T18:51:13.193 に答える