我正在寻找一个解决方案来获取枚举的完整字符串.
例:
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();
Run Code Online (Sandbox Code Playgroud)
那么有解决方案吗?
Ali*_*eza 10
public static class Extensions
{
public static string GetFullName(this Enum myEnum)
{
return string.Format("{0}.{1}", myEnum.GetType().Name, myEnum.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
Color color = Color.Red;
string fullName = color.GetFullName();
Run Code Online (Sandbox Code Playgroud)
注意:我认为GetType().Name
更好GetType().FullName
归档时间: |
|
查看次数: |
5519 次 |
最近记录: |