2014-04-01 30 views
0

我有這個枚舉轉換枚舉用[DescriptionAttribute]到字典<string,int>

public enum Genders 
    { 
     [Description("Nữ")] 
     Female, 
     [Description("Nam")] 
     Male 
    } 

我使用此代碼來獲取每個枚舉名稱和值,並將其保存到一個字典 結果是

女: 0

男性:1

accountViewModel.Genders = Enum.GetValues(typeof(Account.Genders)) 
           .Cast<Account.Genders>() 
           .ToDictionary(t => t.ToString(), t => (int)t); 

如何噸o爲了獲得每個Enum的描述和它的值,

像這樣。

NU:0

南:1

回答

0

根據一個答案https://stackoverflow.com/a/2650090/643095

string description = Enumerations.GetEnumDescription((MyEnum)value); 

哪裏valueint枚舉值。

您可以使用這樣的事情:

accountViewModel.Genders = Enum.GetValues(typeof(Account.Genders)) 
           .Cast<Account.Genders>() 
           .ToDictionary(t => Enumerations.GetEnumDescription((Genders)t), t => (int)t); 
相關問題