String representation of an Enum

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

I have the following enumeration:

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

T

30条回答
  •  -上瘾入骨i
    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 map = new Dictionary();
        public MyClass()
        {
             map.Add(AuthenticationMethod.FORMS,"Forms Authentication");
             map.Add(AuthenticationMethod.WINDOWSAUTHENTICATION ,"Windows Authentication");
             map.Add(AuthenticationMethod.SINGLESIGNON ,"SSo Authentication");
        }
    }
    

提交回复
热议问题