mardi 19 avril 2016

C# Get value of Enum instance

Say I have this enum:

public enum MyEnum{
    ValueOne = 1,
    ValueTwo = 2,
    ValueThree = 3
}

And then this field/variable:

public MyEnum myEnumInstance = MyEnum.ValueTwo;

I need to the the name of myEnumInstance via reflection.

What I tried:

myClassInstance.GetType().GetField("myEnumInstance").GetValue(myClassInstance)

Which always returns ValueOne, no matter what myEnumInstance is set to.

How can I get the string value/name of the enum field via reflection?





Aucun commentaire:

Enregistrer un commentaire