我們有一些可能導出爲各種格式的東西。目前我們已經通過枚舉喜歡這代表這些格式:替換需要翻譯和枚舉的枚舉
[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