String representation of an Enum

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

    Update: Visiting this page, 8 years later, after not touching C# for a long while, looks like my answer is no longer the best solution. I really like the converter solution tied with attribute-functions.

    If you are reading this, please make sure you also check out other answers.
    (hint: they are above this one)


    As most of you, I really liked the selected answer by Jakub Šturc, but I also really hate to copy-paste code, and try to do it as little as I can.

    So I decided I wanted an EnumBase class from which most of the functionality is inherited/built-in, leaving me to focus on the content instead of behavior.

    The main problem with this approach is based on the fact that although Enum values are type-safe instances, the interaction is with the Static implementation of the Enum Class type. So with a little help of generics magic, I think I finally got the correct mix. Hope someone finds this as useful as I did.

    I'll start with Jakub's example, but using inheritance and generics:

    public sealed class AuthenticationMethod : EnumBase
    {
        public static readonly AuthenticationMethod FORMS =
            new AuthenticationMethod(1, "FORMS");
        public static readonly AuthenticationMethod WINDOWSAUTHENTICATION =
            new AuthenticationMethod(2, "WINDOWS");
        public static readonly AuthenticationMethod SINGLESIGNON =
            new AuthenticationMethod(3, "SSN");
    
        private AuthenticationMethod(int Value, String Name)
            : base( Value, Name ) { }
        public new static IEnumerable All
        { get { return EnumBase.All; } }
        public static explicit operator AuthenticationMethod(string str)
        { return Parse(str); }
    }
    

    And here is the base class:

    using System;
    using System.Collections.Generic;
    using System.Linq; // for the .AsEnumerable() method call
    
    // E is the derived type-safe-enum class
    // - this allows all static members to be truly unique to the specific
    //   derived class
    public class EnumBase where E: EnumBase
    {
        #region Instance code
        public T Value { get; private set; }
        public string Name { get; private set; }
    
        protected EnumBase(T EnumValue, string Name)
        {
            Value = EnumValue;
            this.Name = Name;
            mapping.Add(Name, this);
        }
    
        public override string ToString() { return Name; }
        #endregion
    
        #region Static tools
        static private readonly Dictionary> mapping;
        static EnumBase() { mapping = new Dictionary>(); }
        protected static E Parse(string name)
        {
            EnumBase result;
            if (mapping.TryGetValue(name, out result))
            {
                return (E)result;
            }
    
            throw new InvalidCastException();
        }
        // This is protected to force the child class to expose it's own static
        // method.
        // By recreating this static method at the derived class, static
        // initialization will be explicit, promising the mapping dictionary
        // will never be empty when this method is called.
        protected static IEnumerable All
        { get { return mapping.Values.AsEnumerable().Cast(); } }
        #endregion
    }
    

提交回复
热议问题