C# Enum.ToString() with complete name

后端 未结 6 926
眼角桃花
眼角桃花 2020-12-20 17:26

I am searching a solution to get the complete String of an enum.

Example:

Public Enum Color
{
    Red = 1,
    Blue = 2
}
Color color = Color.Red;

/         


        
6条回答
  •  死守一世寂寞
    2020-12-20 17:49

    public static class Extensions
    {
        public static string GetFullName(this Enum myEnum)
        {
          return string.Format("{0}.{1}", myEnum.GetType().Name, myEnum.ToString());
        }
    }
    

    usage:

    Color color = Color.Red;
    string fullName = color.GetFullName();
    

    Note: I think GetType().Name is better that GetType().FullName

提交回复
热议问题