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);
}