-2

I have a list of enum names. This list could be more descriptive to have the enum path such as Library.Base.Enums.{EnumName} if that helps.The enums could be in different folders such as MyEnumCategories could be stored in the folder Library/Base/Enums/Category/ and MyEnumCities could be in Library/Base/Enums/Locations/. I have a growing list of enum names and I need to be able to get all the values stored in the enums from the list. I need to return all the values the enums as shown below:

List<string> enumNames=["MyEnumCategories","MyEnumCities"]
List<string> values = new List<strings>

public enum MyEnumCategories
{
    Service = 0,
    Corporate = 1,
    Enterprise = 2,
    AllSites = 3,
    IndividualSites = 4,
    Site = 5,
    Notification = 6
}
public enum MyEnumCities
{
    Chicago= 0,
    Boston= 1,
    NewYork= 2,
    Denvor= 3,
    Austin= 4,
    Seattle= 5,
    SanFrancisco= 6
}

OUTPUT: values =["Service","Corporate ","Enterprise ",...."Seattle","SanFrancisco",]

  • Can you please [edit] the question to clarify what of many steps to get the result you are blocked on? Hopefully it is not something like "I can't concatenate two lists of strings" (which is last one to get output you like) – Alexei Levenkov Apr 28 '20 at 17:31
  • Possible duplicate of https://stackoverflow.com/a/972323/1260204 – Igor Apr 28 '20 at 17:31
  • Hi Igor. This is different than that since I do not know what the enum could be since I am getting the names from a list of strings. The list of strings has the names of the enum, not the values. – Giancarlo A. Barillas Apr 28 '20 at 18:44
  • "Enums can be in different folders and are stored on a server"? Are they compiled code in your program, or not? – Lasse V. Karlsen Apr 28 '20 at 19:00
  • Hi Lasse. I update the description to give more context. I wanted to explain that the Enums are located in different folders. They are compiled code in my program. – Giancarlo A. Barillas Apr 28 '20 at 19:10

1 Answers1

2

Quick and easy with linq. If you want to span multiple assemblies or add case insensitivity or partial matching, etc, you'll have a few extra bits to add.

var enumNames = new[] { "MyEnumCategories", "MyEnumCities" };
var enumTypes = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsEnum && enumNames.Contains(x.Name));
var values = enumTypes.SelectMany(Enum.GetNames).ToArray();
Chaos
  • 250
  • 2
  • 10