3

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 get the name of myEnumInstance via reflection from another class.

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?

Zong
  • 6,160
  • 5
  • 32
  • 46
Keith M
  • 1,199
  • 2
  • 18
  • 38
  • Possible duplicate of [Enum String Name from Value](http://stackoverflow.com/questions/309333/enum-string-name-from-value) – FiringSquadWitness Apr 20 '16 at 00:25
  • @FiringSquadWitness Nope not a duplicate, my question is totally different. I'm having trouble getting the value to begin with – Keith M Apr 20 '16 at 00:27
  • I'm still unclear on why you need reflection... why not `myClassInstance.myEnumInstance.ToString()`? – Jason P Apr 20 '16 at 00:27
  • @JasonP Tricky to explain, but basically I have an inventory system with several different classes for different types of items. I'm making an editor to display the fields for all the itme different classes. Since i'm programming it to be generic and not to have any class-specific code, I need to use reflection to access to fields. I have the system working for everything but enums. – Keith M Apr 20 '16 at 00:31
  • @KeithM Running your code verbatim does indeed produce `ValueTwo`. It's almost definitely because you're picking up the wrong field, or `myEnumInstance` is not set to the value you think it is. – Rob Apr 20 '16 at 00:34
  • Not sure what you are actually looking for but you can get everything from `Enum`, `Enum.GetValues(typeof(MyEnum))`, `Enum.GetNames(typeof(myEnum))`, and `typeof(myEnum).ToString()`. – gmiley Apr 20 '16 at 00:34
  • @Rob That's odd, I've been working at this for the last hour and it's still returning the wrong value when I know it's set otherwise. It might have to do with the environment I'm working in, I'll try to find out if that's the case. – Keith M Apr 20 '16 at 00:35
  • 1
    @KeithM See [this fiddle](https://dotnetfiddle.net/oWVszc) - it produces the result you're after. I'm leaning towards voting to close this as unable to replicate, as it appears the problem really lies somewhere completely unrelated to the question – Rob Apr 20 '16 at 00:37
  • Just for testing I put the reflection code and `myEnumInstance.ToString()` next to each other, and printed them, and the reflection returned the wrong value and `.ToString()` returned the right value. Something odd is happening here that looks like it doesn't have to do with the reflection and isn't related to the question, so I'll close it – Keith M Apr 20 '16 at 00:40
  • What reflection code are you using? It may be returning the first value in your enum rather than the value thatt the instance is set to. – gmiley Apr 20 '16 at 00:42
  • Sure enough, I just realized I was passing the wrong instance of my class to the reflection. Instead of passing my modified one I was passing an instance I hadn't populated yet. They both had similar field names, so I completely missed it – Keith M Apr 20 '16 at 00:44
  • A case of RUTA? ;) – gmiley Apr 20 '16 at 00:46

1 Answers1

8

You don't need reflection. You just need to call .ToString().

myEnumInstance.ToString();

which will output "ValueTwo";

However, if you insist on using reflection, the following example works just fine:

var myClassInstance = new MyClass();
myClassInstance.GetType()
               .GetField("myEnumInstance")
               .GetValue(myClassInstance);

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

public class MyClass
{
    public MyEnum myEnumInstance = MyEnum.ValueTwo;
}

Note that in C#6 you can also use nameof for some strongly-typed syntactic sugar:

myClassInstance.GetType()
               .GetField(nameof(myEnumInstance))
               .GetValue(myClassInstance);

If you are STILL not able to access the field, it is because it is not public as described in your sample code, in which you'd need to pass in the appropriate binding flags.

myClassInstance
    .GetType()
    .GetField(nameof(myEnumInstance), 
        BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance)
    .GetValue(myClassInstance);
David L
  • 32,885
  • 8
  • 62
  • 93
  • I edited the question to add that I'm accessing this from another class. The way I'm doing this requires reflection. Trust me, I would do `myEnumInstance.ToString();` if it was that simple :) – Keith M Apr 20 '16 at 00:26
  • 2
    You should still be able to call `myClassInstance.myEnumInstance.ToString()` even given your edit? – NStuke Apr 20 '16 at 00:27
  • @KeithM NStuke should be correct based on your edit. You can still call `.ToString()`. – David L Apr 20 '16 at 00:28