lundi 23 décembre 2019

Get Enum member by int value

I am trying to write a System.Enum extension which has the following signature:

public static string GetRepresentation(this Enum e)
{
   ...
}

Imagine an Enum which looks like this:

public enum Foo
{
   Foo = 1,
   Bar = 2
}

When I call the extension e.g. Foo.Bar.GetRepresentation() I want to convert an integer. Lets say 2 to the representative Enum member which would be Foo.Bar. For strings you can do the following:

var enumMember = e.GetType().GetMember("Bar")[0];

So can you get a MemberInfo from an int, when you only have the System.Enum object of the actual enum?





Aucun commentaire:

Enregistrer un commentaire