String representation of an Enum

后端 未结 30 1931
不思量自难忘°
不思量自难忘° 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

    Option 1:

    public sealed class FormsAuth
    {
         public override string ToString{return "Forms Authtentication";}
    }
    public sealed class WindowsAuth
    {
         public override string ToString{return "Windows Authtentication";}
    }
    
    public sealed class SsoAuth
    {
         public override string ToString{return "SSO";}
    }
    

    and then

    object auth = new SsoAuth(); //or whatever
    
    //...
    //...
    // blablabla
    
    DoSomethingWithTheAuth(auth.ToString());
    

    Option 2:

    public enum AuthenticationMethod
    {
            FORMS = 1,
            WINDOWSAUTHENTICATION = 2,
            SINGLESIGNON = 3
    }
    
    public class MyClass
    {
        private Dictionary<AuthenticationMethod, String> map = new Dictionary<AuthenticationMethod, String>();
        public MyClass()
        {
             map.Add(AuthenticationMethod.FORMS,"Forms Authentication");
             map.Add(AuthenticationMethod.WINDOWSAUTHENTICATION ,"Windows Authentication");
             map.Add(AuthenticationMethod.SINGLESIGNON ,"SSo Authentication");
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:28

    If you think about the problem we're trying to solve, it's not an enum we need at all. We need an object that allows a certain number of values to be associated with eachother; in other words, to define a class.

    Jakub Šturc's type-safe enum pattern is the best option I see here.

    Look at it:

    • It has a private constructor so only the class itself can define the allowed values.
    • It is a sealed class so values can't be modifed through inheritence.
    • It is type-safe, allowing your methods to require only that type.
    • There is no reflection performance hit incurred by accessing the values.
    • And lastly, it can be modified to associate more than two fields together, for example a Name, Description, and a numeric Value.
    0 讨论(0)
  • 2020-11-22 03:30

    I use a combination of several of the suggestions above, combined with some caching. Now, I got the idea from some code that I found somewhere on the net, but I can neither remember where I got it or find it. So if anyone ever finds something that looks similar please comment with the attribution.

    Anyway, the usage involves the type converters, so if you are binding to the UI it 'just works'. You can extended with Jakub's pattern for quick code lookup by initializing from the type converter into the static methods.

    The base usage would look like this

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

    The code for the custom enum type converter follows:

    public class CustomEnumTypeConverter<T> : EnumConverter
        where T : struct
    {
        private static readonly Dictionary<T,string> s_toString = 
          new Dictionary<T, string>();
    
        private static readonly Dictionary<string, T> s_toValue = 
          new Dictionary<string, T>();
    
        private static bool s_isInitialized;
    
        static CustomEnumTypeConverter()
        {
            System.Diagnostics.Debug.Assert(typeof(T).IsEnum,
              "The custom enum class must be used with an enum type.");
        }
    
        public CustomEnumTypeConverter() : base(typeof(T))
        {
            if (!s_isInitialized)
            {
                Initialize();
                s_isInitialized = true;
            }
        }
    
        protected void Initialize()
        {
            foreach (T item in Enum.GetValues(typeof(T)))
            {
                string description = GetDescription(item);
                s_toString[item] = description;
                s_toValue[description] = item;
            }
        }
    
        private static string GetDescription(T optionValue)
        {
            var optionDescription = optionValue.ToString();
            var optionInfo = typeof(T).GetField(optionDescription);
            if (Attribute.IsDefined(optionInfo, typeof(DescriptionAttribute)))
            {
                var attribute = 
                  (DescriptionAttribute)Attribute.
                     GetCustomAttribute(optionInfo, typeof(DescriptionAttribute));
                return attribute.Description;
            }
            return optionDescription;
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, 
           System.Globalization.CultureInfo culture, 
           object value, Type destinationType)
        {
            var optionValue = (T)value;
    
            if (destinationType == typeof(string) && 
                s_toString.ContainsKey(optionValue))
            {
                return s_toString[optionValue];
            }
    
            return base.ConvertTo(context, culture, value, destinationType);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, 
           System.Globalization.CultureInfo culture, object value)
        {
            var stringValue = value as string;
    
            if (!string.IsNullOrEmpty(stringValue) && s_toValue.ContainsKey(stringValue))
            {
                return s_toValue[stringValue];
            }
    
            return base.ConvertFrom(context, culture, value);
        }
    }
    

    }

    0 讨论(0)
  • 2020-11-22 03:30

    I agree with Keith, but I can't vote up (yet).

    I use a static method and swith statement to return exactly what I want. In the database I store tinyint and my code only uses the actual enum, so the strings are for UI requirements. After numerous testing this resulted in the best performance and most control over the output.

    public static string ToSimpleString(this enum)
    {
         switch (enum)
         {
             case ComplexForms:
                 return "ComplexForms";
                 break;
         }
    }
    
    public static string ToFormattedString(this enum)
    {
         switch (enum)
         {
             case ComplexForms:
                 return "Complex Forms";
                 break;
         }
    }
    

    However, by some accounts, this leads to a possible maintenance nightmare and some code smell. I try to keep an eye for enums that are long and a lot of enums, or those that change frequently. Otherwise, this has been a great solution for me.

    0 讨论(0)
  • 2020-11-22 03:31

    Very simple solution to this with .Net 4.0 and above. No other code is needed.

    public enum MyStatus
    {
        Active = 1,
        Archived = 2
    }
    

    To get the string about just use:

    MyStatus.Active.ToString("f");
    

    or

    MyStatus.Archived.ToString("f");`
    

    The value will be "Active" or "Archived".

    To see the different string formats (the "f" from above) when calling Enum.ToString see this Enumeration Format Strings page

    0 讨论(0)
  • 2020-11-22 03:31

    My variant

    public struct Colors
    {
        private String current;
    
        private static string red = "#ff0000";
        private static string green = "#00ff00";
        private static string blue = "#0000ff";
    
        private static IList<String> possibleColors; 
    
        public static Colors Red { get { return (Colors) red; } }
        public static Colors Green { get { return (Colors) green; } }
        public static Colors Blue { get { return (Colors) blue; } }
    
        static Colors()
        {
            possibleColors = new List<string>() {red, green, blue};
        }
    
        public static explicit operator String(Colors value)
        {
            return value.current;
        }
    
        public static explicit operator Colors(String value)
        {
            if (!possibleColors.Contains(value))
            {
                throw new InvalidCastException();
            }
    
            Colors color = new Colors();
            color.current = value;
            return color;
        }
    
        public static bool operator ==(Colors left, Colors right)
        {
            return left.current == right.current;
        }
    
        public static bool operator !=(Colors left, Colors right)
        {
            return left.current != right.current;
        }
    
        public bool Equals(Colors other)
        {
            return Equals(other.current, current);
        }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (obj.GetType() != typeof(Colors)) return false;
            return Equals((Colors)obj);
        }
    
        public override int GetHashCode()
        {
            return (current != null ? current.GetHashCode() : 0);
        }
    
        public override string ToString()
        {
            return current;
        }
    }
    

    Code looks a bit ugly, but usages of this struct are pretty presentative.

    Colors color1 = Colors.Red;
    Console.WriteLine(color1); // #ff0000
    
    Colors color2 = (Colors) "#00ff00";
    Console.WriteLine(color2); // #00ff00
    
    // Colors color3 = "#0000ff"; // Compilation error
    // String color4 = Colors.Red; // Compilation error
    
    Colors color5 = (Colors)"#ff0000";
    Console.WriteLine(color1 == color5); // True
    
    Colors color6 = (Colors)"#00ff00";
    Console.WriteLine(color1 == color6); // False
    

    Also, I think, if a lot of such enums required, code generation (e.g. T4) might be used.

    0 讨论(0)
提交回复
热议问题