0

I have an enum like this:

public static class ExampleClass {
    public enum ExampleEnum {
        None,
        Example0,
        Example1
    }
}

and i only got string of type and value. like:

const string Type = "ExampleClass.ExampleEnum";
const string Value = "Example0";

and i don't have the "Type" of enum, so i can't use like typeof(ExampleClass.ExampleEnum).

in this case, can i check if enum "ExampleClass.ExampleEnum" exists, and then "ExampleClass.ExampleEnum" has "Example0"?

if above is possible, can i get an object or index of specific enum value?

Tempus
  • 97
  • 1
  • 1
  • 8
  • You can get the type as follows: https://stackoverflow.com/questions/20008503/get-type-by-name and then get the value as follows https://stackoverflow.com/questions/21561684/get-value-of-enum-member-by-its-name – derpirscher Jan 14 '22 at 08:16
  • [Enum string name from value](https://stackoverflow.com/questions/309333/enum-string-name-from-value) check the 2nd answer. – Ergis Jan 14 '22 at 08:32

1 Answers1

0

You can use reflection to get all enums in the assembly,

And then you could use Enum.IsDefined to check whether the value exists on a particular enum.

https://learn.microsoft.com/en-us/dotnet/api/system.enum.isdefined?view=net-6.0

sommmen
  • 6,570
  • 2
  • 30
  • 51