0

Here is some test code:

public enum ItemType : byte 
{
    [EnumMember(Value = "Computers & Electronics")]
    ComputersAndElectronics,
    [EnumMember(Value = "Music/Movies")]
    MusicMovies,
    Other
}

public void Test()
{
    string testString1 = "Computers & Electronics";
    ItemType itTest1 = ???  // should be assigned "ItemType.ComputersAndElectronics"
    string testString2 = "Other";
    ItemType itTest2 = ???  // should be assigned "ItemType.Other"
}

The values I will be getting from an outside source are the strings in the EnumMember attributes (except for the last enum value, which can be used directly as a parameter name)

I'm not sure how to parse the expanded strings into the Enum type. Enum.Parse only seems to parse a string containing the actual C# parameter name for the enum value (e.g. `"ComputersAndElectronics" will properly parse, but "Computers & Electronics" will not.)

The "Other" case is important since that item does not have an EnumMember attribute.

fdmillion
  • 4,823
  • 7
  • 45
  • 82
  • I think these links would be helpful: [Compare enums by names](https://stackoverflow.com/questions/45702404/c-sharp-enum-names-to-filter-on-in-a-dropdownlist-instead-of-value/45702532#45702532) and [Get enums attribute](https://stackoverflow.com/questions/45426266/get-description-attributes-from-a-flagged-enum/) – George Alexandria Aug 19 '17 at 16:18
  • The key was using `Description`. I found this post https://stackoverflow.com/questions/4249632/string-to-enum-with-description which explains how to do it. I was using EnumValue because I was copy/pasting from some other code where I needed to properly serialize/deserialize to JSON. – fdmillion Aug 19 '17 at 16:43

0 Answers0