我想知道如何在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 :(得分:10)
参数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;
答案 1 :(得分:1)
关于你的第一条评论,我不知道Filehelpers,但我使用过的每个图书馆都要求你指定你想要识别/解析的格式。 .Net的DateTime工具也不例外。但是,.Net确实提供了一个有用的函数,可以返回所有内置格式,然后可以使用TryParse()
(documentation)进行迭代。
例如,以下代码使用内置DateTimeFormatInfo.GetAllDateTimeFormats()
(documentation here)来循环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);
}
}
}
}
}