Skip to content

Instantly share code, notes, and snippets.

@cullen-tsering
Last active August 29, 2015 14:04
Show Gist options
  • Save cullen-tsering/951c1048504630b596db to your computer and use it in GitHub Desktop.
Save cullen-tsering/951c1048504630b596db to your computer and use it in GitHub Desktop.
Get Constant value and names in a dictionary
public class DoingConstants
{
public const string Flying = "F";
public const string Walking = "W";
public const string Crawling = "C";
public const string Scooting = "R";
static Dictionary<string,string> _asDictionary;
public static Dictionary<string, string> AsDictionary
{
get {
if (_asDictionary==null)
{
_asDictionary = new Dictionary<string, string>();
var t = new DoingConstants();
foreach (var type in typeof(DoingConstants).GetConsts())
{
toReturn.Add(type.GetValue(t).ToString(),type.Name);
}
}
}
return _asDictionary;
}
}
}
publich static Helper
{
public static List<FieldInfo> GetConsts(this Type type)
{
return type.GetFields
(
BindingFlags.Public
| BindingFlags.Static
| BindingFlags.FlattenHierarchy
)
.Where(fi => fi.IsLiteral && !fi.IsInitOnly)
.ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment