Enum to Dictionary in C#

前端 未结 10 2047
攒了一身酷
攒了一身酷 2020-12-02 15:10

I have searched this online, but I can\'t find the answer I am looking for.

Basically I have the following enum:

public enum typFoo : int
{
   itemA         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 15:47

    Using reflection:

    Dictionary mydic = new Dictionary();
    
    foreach (FieldInfo fi in typeof(typFoo).GetFields(BindingFlags.Public | BindingFlags.Static))
    {
        mydic.Add(fi.GetRawConstantValue(), fi.Name);
    }
    

提交回复
热议问题