我正在寻找一个解决方案来获取枚举的完整字符串.
例:
Public Enum Color { Red = 1,Blue = 2 } Color color = Color.Red; // This will always get "Red" but I need "Color.Red" string colorString = color.ToString(); // I know that this is what I need: colorString = Color.Red.ToString();
那么有解决方案吗?
解决方法
public static class Extensions { public static string GetFullName(this Enum myEnum) { return string.Format("{0}.{1}",myEnum.GetType().Name,myEnum.ToString()); } }
用法:
Color color = Color.Red; string fullName = color.GetFullName();
注意:我认为GetType().Name比GetType().FullName更好