Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String representation of an Enum

Tags:

c#

enums

I have the following enumeration:

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

The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id 1.

I have found the following solution for this problem (link):

First I need to create a custom attribute called "StringValue":

public class StringValue : System.Attribute {     private readonly string _value;      public StringValue(string value)     {         _value = value;     }      public string Value     {         get { return _value; }     }  } 

Then I can add this attribute to my enumerator:

public enum AuthenticationMethod {     [StringValue("FORMS")]     FORMS = 1,     [StringValue("WINDOWS")]     WINDOWSAUTHENTICATION = 2,     [StringValue("SSO")]     SINGLESIGNON = 3 } 

And of course I need something to retrieve that StringValue:

public static class StringEnum {     public static string GetStringValue(Enum value)     {         string output = null;         Type type = value.GetType();          //Check first in our cached results...          //Look for our 'StringValueAttribute'           //in the field's custom attributes          FieldInfo fi = type.GetField(value.ToString());         StringValue[] attrs =            fi.GetCustomAttributes(typeof(StringValue),                                    false) as StringValue[];         if (attrs.Length > 0)         {             output = attrs[0].Value;         }          return output;     } } 

Good now I've got the tools to get a string value for an enumerator. I can then use it like this:

string valueOfAuthenticationMethod = StringEnum.GetStringValue(AuthenticationMethod.FORMS); 

Okay now all of these work like a charm but I find it a whole lot of work. I was wondering if there is a better solution for this.

I also tried something with a dictionary and static properties but that wasn't better either.

like image 282
user29964 Avatar asked Jan 08 '09 14:01

user29964


People also ask

Can an enum be a string?

In a string enum, each member has to be constant-initialized with a string literal, or with another string enum member. While string enums don't have auto-incrementing behavior, string enums have the benefit that they “serialize” well.

Can we give string value in enum?

Since C# doesn't support enum with string value, in this blog post, we'll look at alternatives and examples that you can use in code to make your life easier. The most popular string enum alternatives are: Use a public static readonly string. Custom Enumeration Class.

What happens when you toString an enum?

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Is enum string or int?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.


2 Answers

Try type-safe-enum pattern.

public sealed class AuthenticationMethod {      private readonly String name;     private readonly int value;      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){         this.name = name;         this.value = value;     }      public override String ToString(){         return name;     }  } 

Update Explicit (or implicit) type conversion can be done by

  • adding static field with mapping

    private static readonly Dictionary<string, AuthenticationMethod> instance = new Dictionary<string,AuthenticationMethod>(); 
    • n.b. In order that the initialisation of the the "enum member" fields doesn't throw a NullReferenceException when calling the instance constructor, be sure to put the Dictionary field before the "enum member" fields in your class. This is because static field initialisers are called in declaration order, and before the static constructor, creating the weird and necessary but confusing situation that the instance constructor can be called before all static fields have been initialised, and before the static constructor is called.
  • filling this mapping in instance constructor

    instance[name] = this; 
  • and adding user-defined type conversion operator

    public static explicit operator AuthenticationMethod(string str) {     AuthenticationMethod result;     if (instance.TryGetValue(str, out result))         return result;     else         throw new InvalidCastException(); } 
like image 187
Jakub Šturc Avatar answered Oct 07 '22 03:10

Jakub Šturc


Use method

Enum.GetName(Type MyEnumType,  object enumvariable)   

as in (Assume Shipper is a defined Enum)

Shipper x = Shipper.FederalExpress; string s = Enum.GetName(typeof(Shipper), x); 

There are a bunch of other static methods on the Enum class worth investigating too...

like image 39
Charles Bretana Avatar answered Oct 07 '22 05:10

Charles Bretana