0

Here's what I've done so far

public enum TCountryNames
{
  [Display(Name="America")]
  cnUSA = 1,
  [Display(Name="England")]
  cnUK,
  [Display(Name="CHINA")]
  cnCHN
}

public class MyClass
{
  public static List<KeyValuePair<string, int>> GetEnumList()
    {
        var list = new List<KeyValuePair<string, int>>();
        foreach (var e in Enum.GetValues(typeof(TCountryNames)))
        {
            list.Add(new KeyValuePair<string, int>(e.ToString(), (int)e));
        }
        return list;
    }
}

Result: [cnUSA,1] with total count 3 and without header

The result i want is [{"Id":1,"Name":"America"},{"Id":2,"Name":"England"}]

I've tried [JsonConverter(typeof(StringEnumConverter))] public TCountryNames Names{ get; set; }

I've also tried converting enum to array list var names = Enum.GetValues(typeof(TCountryNames)); ArrayList arrLst = new ArrayList() { names }; but both of them doesn't seems to be working.

*Any help will be appreciated. Thank You in Advance. *

2
  • Accessing enum attributes; stackoverflow.com/a/71247074/4139809 Commented Mar 4, 2022 at 5:05
  • @jeremyLakeman, it will give me a display name but how do I compose the header as well? Commented Mar 4, 2022 at 5:35

2 Answers 2

2
  1. If you don't want to add new class

     public static List<Dictionary<string, object>> GetEnumList()
     {
         var list = new List<Dictionary<string, object>>();
         foreach (var e in Enum.GetValues(typeof(TCountryNames)))
         {
             list.Add(new Dictionary<string, object> { { "Id", (int)e }, { "Name", e.ToString() } });
         }
         return list;
     }
    
  2. Define a model for serialization

     public class EnumData
     {
         public int Id { get; set; }
         public string Name { get; set; }
     }
    
     public static List<EnumData> GetEnumList()
     {
         var list = new List<EnumData>();
         foreach (var e in Enum.GetValues(typeof(TCountryNames)))
         {
             list.Add(new EnumData { Id = (int)e, Name = e.ToString() });
         }
         return list;
     }
    
Sign up to request clarification or add additional context in comments.

Comments

1

For get display name value you should use System.Reflection. And then you could do this in simple way:

   public enum TCountryNames
    {
        [Display(Name = "America")]
        cnUSA = 1,
        [Display(Name = "England")]
        cnUK,
        [Display(Name = "CHINA")]
        cnCHN
    }

public class EnumData
{
    public int Id { get; set; }
    public string? Name { get; set; }
}

public class MyClass
{
    public static List<EnumData> GetEnumList()
    {
        var list = new List<EnumData>();
        foreach (var e in Enum.GetValues(typeof(TCountryNames)))
        {
            list.Add(new EnumData
            { 
                Id = (int)e, 
                Name = e.GetType()
                    .GetMember(e.ToString())
                    .First()?
                    .GetCustomAttribute<DisplayAttribute>()?
                    .GetName()
            });
        }
        return list;
    }
}

So to clarify:

  • you create loop foreach enum
  • take id by casting
  • take name using reflaction - I added all needed protection against null exception

Output: [ { "Id": 1, "Name": "America" }, { "Id": 2, "Name": "England" }, { "Id": 3, "Name": "CHINA" } ]

Example: https://dotnetfiddle.net/XVL2LI

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.