2012-02-03 17 views
5

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); 
} 
+0

私はfilehelpersに慣れていないんだけど、あなたが使用したくない理由があります.NETのSystem.DateTimeクラスの組み込み日時解析機能 – kmote

+0

'FieldConverterAttribute(ConverterKind、params string [])'コンストラクタがオーバーロードされたバージョンをお持ちですか?古いバージョンでは、組み込みコンバージョンには3つ以上のパラメータが使用できないため(サポートしているものもあれば、文字列配列で手作業でラップする必要があるものもあります。少なくともコンパイラのエラーはその理由のようです... – Nuffin

+0

あなたの質問をありがとう - それは2つの異なる日付形式の2つのファイルを持っている私の問題を解決したが、FileHelpersは2 NuGetパッケージとして存在するという事実を私に警告した2.0.0、もう1つは2.9.9です。もちろん、ウェブサイトには記載されていません。 Grrr ... – Colin

答えて

10

パラメータFieldConverterAttribute(ConverterKind, params string[])と過負荷はありません。

FieldConverterAttribute(ConverterKind, string, string, string)に1つありますので、最大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; 
1

ようになり、私はしないでくださいFilehelpersについて知っていますが、私が扱っているすべてのライブラリでは、認識/解析する書式を指定する必要があります。 .NetのDateTime機能は違いはありません。ただし、.Netは組み込みのすべての形式を返すことができる便利な関数を提供していますが、TryParse()documentation)で反復処理することができます。

たとえば、次のコードでは、組み込みのDateTimeFormatInfo.GetAllDateTimeFormats()documentation here)を使用して128のカスタムdatetime形式をループします。 (このコードは、「往復」を示しています。文字列に日付を変換して文字列をパース):

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); 
      } 
     } 
     } 
    } 
} 
関連する問題