我们有一些可能导出为各种格式的东西。目前我们已经通过枚举喜欢这代表这些格式:替换需要翻译和枚举的枚举
[Flags]
public enum ExportFormat
{
None = 0x0,
Csv = 0x1,
Tsv = 0x2,
Excel = 0x4,
All = Excel | Csv | Tsv
}
问题是,这些必须被枚举,他们还需要在用户界面翻译或说明。目前我通过创建两个扩展方法解决了这个问题他们工作,但我真的不喜欢他们或解决方案...他们觉得有点臭。问题是我真的不知道如何做得更好。有没有人有任何好的选择?这两种方法:
public static IEnumerable<ExportFormat> Formats(this ExportFormat exportFormats)
{
foreach (ExportFormat e in Enum.GetValues(typeof (ExportFormat)))
{
if (e == ExportFormat.None || e == ExportFormat.All)
continue;
if ((exportFormats & e) == e)
yield return e;
}
}
public static string Describe(this ExportFormat e)
{
var r = new List<string>();
if ((e & ExportFormat.Csv) == ExportFormat.Csv)
r.Add("Comma Separated Values");
if ((e & ExportFormat.Tsv) == ExportFormat.Tsv)
r.Add("Tab Separated Values");
if ((e & ExportFormat.Excel) == ExportFormat.Excel)
r.Add("Microsoft Excel 2007");
return r.Join(", ");
}
也许这是这样做的方式,但我有一种感觉,必须有更好的方法来做到这一点。我怎么能重构这个?
难道你不需要本地化这些字符串吗?如果是这样,他们将会在资源文件中,所以将它们放在代码中没有意义。 – 2009-07-09 08:54:47
是的。但仍然需要一些连接资源键和枚举的方式。 – Svish 2009-07-09 09:28:45