Last active
August 29, 2015 14:04
-
-
Save cullen-tsering/951c1048504630b596db to your computer and use it in GitHub Desktop.
Get Constant value and names in a dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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