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",]
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.