String representation of an Enum

后端 未结 30 1934
不思量自难忘°
不思量自难忘° 2020-11-22 02:44

I have the following enumeration:

public enum AuthenticationMethod
{
    FORMS = 1,
    WINDOWSAUTHENTICATION = 2,
    SINGLESIGNON = 3
}

T

30条回答
  •  终归单人心
    2020-11-22 03:27

    I wanted to post this as a comment to the post quoted below but couldn't because I don't have enough rep - so please don't down-vote. The code contained an error and I wanted to point this out to individuals trying to use this solution:

    [TypeConverter(typeof(CustomEnumTypeConverter(typeof(MyEnum))]
    public enum MyEnum
    {
      // The custom type converter will use the description attribute
      [Description("A custom description")]
      ValueWithCustomDescription,
      // This will be exposed exactly.
      Exact
    }
    

    should be

    [TypeConverter(typeof(CustomEnumTypeConverter))]
    public enum MyEnum
    {
      // The custom type converter will use the description attribute
      [Description("A custom description")]
      ValueWithCustomDescription,
    
      // This will be exposed exactly.
      Exact
    }
    

    Brillant!

提交回复
热议问题