2012-02-03 18 views
5

Zastanawiam się, jak zrobić wiele dat w filehelpers? Wygląda na to, że musisz określić każdy format, który zamierzasz obsłużyć (co jest do bani ... chciałby obsłużyć więcej podstawowych).Wielokrotne datowanie w plikuOpisz jak?

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

To daje mi chociaż

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

Więc wydaje się, że muszę zrobić jakiś niestandardowy konwersji lub coś? Czy to jest poprawne?

Edit

używam wersji 2.9.9.0

Opcje

// 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

Nie jestem zaznajomiony z filehelpers, ale czy istnieje powód, dla którego nie chcesz używać wbudowana funkcja analizowania datetime klasy System.DateTime .Net? – kmote

+0

Czy jesteś pewien, że masz wersję z przeciążeniem konstruktora 'FieldConverterAttribute (ConverterKind, string łańcuchów znaków [])'? Ponieważ starsze wersje nie zezwalają na więcej niż 3 parametry dla wbudowanych konwersji (niektóre mogą je obsługiwać, ale wymagają ręcznego zawijania w tablicy ciągów znaków - Nie jestem tego pewien, ponieważ nie badałem tak daleko). Przynajmniej błąd kompilatora wygląda tak, dlatego ... – Nuffin

+0

Dziękuję za twoje pytanie - rozwiązał mój problem (mając 2 różne formaty daty dla 2 plików), ale także ostrzegł mnie, że FileHelpers istnieje jako 2 pakiety NuGet - jeden dla 2.0.0 i inne dla 2.9.9 - oczywiście nie wymienione na stronie internetowej. Grrr ... – Colin

Odpowiedz

10

Nie ma przeciążenie z parametrami FieldConverterAttribute(ConverterKind, params string[]).

Jest jeden z FieldConverterAttribute(ConverterKind, string, string, string), dzięki czemu można dostarczyć do 3 formatów.

Jeśli potrzebujesz więcej, wówczas można stworzyć swój własny konwerter:

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

I wówczas pole będzie wyglądać

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

Odnośnie do pierwszego komentarza, nie wiem wiem o Filehelpers, ale każda biblioteka, z którą pracowałem, wymaga określenia formatów, które chcesz rozpoznać/przeanalizować. Funkcje DateTime w sieci .Net nie różnią się. Jednak .Net zapewnia użyteczną funkcję, która może zwrócić wszystkie wbudowane formaty, które można następnie iterować za pomocą TryParse() (documentation).

Na przykład poniższy kod używa wbudowanego DateTimeFormatInfo.GetAllDateTimeFormats() (documentation here) do zapętlenia 128 niestandardowych formatów datetime. (Ten kod demonstruje „round-trip”: przekształcaniu datę do strun, a następnie analizowania ciągów):

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