C# Enum.ToString() with complete name

后端 未结 6 934
眼角桃花
眼角桃花 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:46

    You can use extension methods.

    public static class EnumExtension
    {
        public static string ToCompleteName(this Color c)
        {
            return "Color." + c.ToString();
        }
    }
    

    Now method below will return "Color.Red".

    color.ToCompleteName();
    

    http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

提交回复
热议问题