1

I have implemented the Type safe enum pattern in our solution, and now I want to get the name of the fields in string format. How do I achieve that? This is my type safe enum class:

    public sealed class Statuses
    {
      private readonly String _name;
      private static readonly Dictionary<string, Statuses> Instance 
= new Dictionary<string, Statuses>();

      private Statuses(string name)
      {
          _name = name;

          Instance[name] = this;
      }

      public static explicit operator Statuses(string str)
      {
          Statuses result;
          if (Instance.TryGetValue(str, out result))
              return result;
          else
              throw new InvalidCastException();
      }


     public static readonly Statuses Submitted = new Statuses("Submitted by user");
     public static readonly Statuses Selected = new Statuses("Selected by user");

     public static IEnumerable<Statuses> AllStatuses
    {
        get
        {
            return Instance.Values;

        }
    }
    public override String ToString()
    {
        return _name;
    }

And I would like to add a property to return the name of the current field like this:

 public string StateCode
        {
            get
            {
                return //The current fields name
            }
        }

Usage:

[TestMethod]
public void TestGetStatusName()
{
var state = Statuses.Selected;
Assert.AreEqual("Selected", state.StateCode);            
}
Fontanka16
  • 1,161
  • 1
  • 9
  • 37
  • possible duplicate of [How to get a list of member names from Enum?](http://stackoverflow.com/questions/3442346/how-to-get-a-list-of-member-names-from-enum) – James Apr 20 '15 at 15:34
  • I am not entirely sure what you are asking here.. in c# an enum is `enum Blah { Blah1,Blah2}`... where exactly do you expect the StateCode to come from? one of the `static readonly` `Submitted`/`Selected` fields and that it would report back 'submitted' or 'selected' ? – Michael Coxon Apr 20 '15 at 15:41
  • It honestly sounds like a really, really complicated way of doing this http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute – Michael Coxon Apr 20 '15 at 15:42
  • To get the variable name try this http://stackoverflow.com/questions/9801624/get-name-of-a-variable-or-parameter – Michael Coxon Apr 20 '15 at 15:46
  • Do you want the variable name or do you want a short form of the field? In this case they are the same but the method to determine them are completely different. For instance in the link you provided they provide two strings to the constructor, the short and long form of the enum. – Guvante Apr 20 '15 at 19:40
  • I made an edit to the question to reflect the fact that it is about the Type Safe Enum Pattern and not enums. – Fontanka16 Apr 20 '15 at 19:40
  • @Guvante: I do not really understand your question, but I want the variable (field) name. Not the value of the _name variable – Fontanka16 Apr 20 '15 at 20:12

2 Answers2

1

There might be some more effective way, but this is the solution i found:

public string StateCode
{
    get
    {
        return GetType()
            .GetFields(BindingFlags.Public | BindingFlags.Static)
            .First(f => f.FieldType == typeof (Statuses) && ToString() == f.GetValue(null).ToString())
            .Name;
    }
}

Edit: changed the answer according to the suggestion below by @Guvante

Fontanka16
  • 1,161
  • 1
  • 9
  • 37
  • 1
    `Where(f => f.FieldType == typeof(Statuses) && ToString() == f.GetValue(null).ToString()).Select(f.Name).First()` is a more compact and direct way of accomplishing that. Note that casting the value to `Statuses` isn't necessary since `ToString` is defined in `object`. – Guvante Apr 20 '15 at 20:27
0

I would follow the way that the link you gave did, pass in the field name as a second string.

private Statuses(string name, string description)

public static readonly Statuses Submitted = new Statuses("Submitted", "Submitted by user");

The only problem then becomes ensuring you are consistently using either the description or the name (in your question you are using the description).

Guvante
  • 18,775
  • 1
  • 33
  • 64