Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way to get Enum with string value in C#?

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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://docs.microsoft.com/en-us/dotnet/api/system.enum.isdefined?view=net-6.0

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading