2011-11-18 18 views
3

我的應用程序中有很多枚舉。他們大多是用在連擊這樣的:使用本地化的枚舉作爲數據源

Enum.GetValues(typeof(TipoControlador)) 

現在,我想本地化他們是這樣的:Localizing enum descriptions attributes

我怎樣才能將它們組合起來?我首先想到的是重寫ToString法的擴展方法,但是這是不可能的=(

+2

迭戈,什麼阻止你應用這種方法:http://stackoverflow.com/questions/569298/localizing-enum-descriptions-attributes/571555#571555? –

+0

*我怎樣才能把它們結合起來?*你是什麼意思?製作標記的枚舉值的字符串表示形式? –

+0

如何更改行'someControl.DataSource = Enum.GetValues(typeof(TipoControlador));'使用該方法? – Diego

回答

1

使用其他物品作爲基礎,你可以創建一個擴展方法是這樣的:

public static class LocalizedEnumExtensions 
{ 
    private static ResourceManager _resources = new ResourceManager("MyClass.myResources", 
     System.Reflection.Assembly.GetExecutingAssembly()); 

    public static IEnumerable<string> GetLocalizedNames(this IEnumerable enumValues) 
    { 
     foreach(var e in enumValues) 
     { 
      string localizedDescription = _resources.GetString(String.Format("{0}.{1}", e.GetType(), e)); 
      if(String.IsNullOrEmpty(localizedDescription)) 
      { 
       yield return e.ToString(); 
      } 
      else 
      { 
       yield return localizedDescription; 
      } 
     } 
    } 
} 

你會使用這樣的:

Enum.GetValues(typeof(TipoControlador)).GetLocalizedNames(); 

從技術上講,這種擴展方法將接受任何數組,你不能限制爲僅工作在一個枚舉,但如果你覺得這是你可以添加擴展方法內部加了額外的驗證重要的是:

if(!e.GetType().IsEnum) throw new InvalidOperationException(String.Format("{0} is not a valid Enum!", e.GetType())); 
+0

如果我這樣做,那麼我將無法將combo.Value轉換爲枚舉,因爲它會有一個字符串。 – Diego

1

這裏有兩個問題,第一個是如何定位由Localizing enum descriptions attributes解決的枚舉。

第二個是如何在使用枚舉值的同時顯示本地化名稱。這可以通過創建一個簡單的包裝對象,如解決:

public sealed class NamedItem 
{ 
    private readonly string name; 
    private readonly object value; 

    public NamedItem (string name, object value) 
    { 
     this.name = name; 
     this.value = value; 
    } 

    public string Name { get { return name; } } 
    public object Value { get { return value; } } 

    public override string ToString() 
    { 
     return name; 
    } 
} 

這提供了一個通用的可重複使用的類的任何下拉框,你可能想顯示一個不同的名稱比本身提供的項目的項目(例如枚舉,整數等)。

一旦你有了這個類,你可以設置下拉的DisplayMemberNameValueMemberValue。這將意味着dropdown.SelectedValue仍然會返回您的枚舉。

+0

我不喜歡這裏是對象而不是特定類型。 – diimdeep

+0

它並沒有太大的區別,因爲dropdown.SelectedValue返回一個對象,但是如果你想更具體一些,它可以很容易地改變成一個泛型類型。 –

0

我知道這個問題很舊,但這可能會幫助一些人。 您只需處理ComboBox控件的格式化事件(http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.format.aspx),並在其中添加文本邏輯。

private void ComboBoxFormat(object sender, ListControlConvertEventArgs e) 
    { 
     e.Value = GetDescription(e.Value); 
    } 

    public static string GetDescription(object item) 
    { 
     string desc = null; 

     Type type = item.GetType(); 
     MemberInfo[] memInfo = type.GetMember(item.ToString()); 
     if (memInfo != null && memInfo.Length > 0) 
     { 
      object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); 
      if (attrs != null && attrs.Length > 0) 
      { 
       desc = (attrs[0] as DescriptionAttribute).Description; 
      } 
     } 

     if (desc == null) // Description not found 
     { 
      desc = item.ToString(); 
     } 

     return desc; 
    } 

有了這個,ComboBox控件仍然保留枚舉值而不是字符串。