-1

I found plenty of solutions for Enum-Parsing into given types and given enums. But I wonder what shall I do when I only have the Name of the Enum I want the values? My current solution for this is a pretty dumb switch-case scenario That does the following:

string[] result = new string[0];

            switch (lvKategorie.SelectedItem.ToString())
            {
                case "Warten":
                    result = Enum.GetNames(typeof(Warten));
                    break;

                case "Maus":
                    result = Enum.GetNames(typeof(Maus));
                    break;

I now wish a better Method because my switch case is getting way too long. I only get the Name of the Enum by a string. This is being generated by my program before and I don't even know the name of the Enum I want to get before this point of my program. So how do I choose in-code which Enum shall be taken to get the values of? (In case my solution is also parsing I just did not get it right and need FURTHER explanations on how this works.)

To be absolutely clear: I want to replace the "typeof(xyz)"-parts in the Enum.GetNames-Methods with an automatic pick of the correct enum by the string of it's name.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222

1 Answers1

-1

Thanks! https://stackoverflow.com/questions/12270238/how-to-get-value-of-an-unknown-enum-by-name-of-enum-and-name-of-value

This and your answer made me recognizing I did not work with the fully qualified Namespace which lead to always null on the Type.GetType-Method.

This now is my one-row solution:

string[] result = Enum.GetNames(Type.GetType("[NamespaceName]." + lvKategorie.SelectedItem.ToString()));