Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

more elegant design about enum

Tags:

c#

enums

I'm on learning for C#.

I heared C# is one of the most constructible language. so would you guys make my code more elegant and efficient?

public class ISO639
{
    public enum ISO639Code
    {
        Afrikaans,                      //af
        Albanian,                       //sq
        Amharic,                        //am

        ...

        Yiddish,                        //yi
        Unknown                         
    }


    public static string GetISO639CodeString(ISO639.ISO639Code l)
    {
        switch (l)
        {
            case ISO639Code.English: return "en";
            case ISO639Code.Japanese: return "ja";

            ...

            case ISO639Code.Hebrew: return "he";
            default: return "";
        }


    public static ISO639.ISO639Code GetISO39CodeValue(string s)
    {

        switch (s)
        {
            case "ko" : return ISO639Code.Korean;
            case "en" : return ISO639Code.English;

            ...

            case "hu" : return ISO639Code.Hungarian;
            default: return ISO639Code.Unknown;
        }
    }
}

Here is a my class ISO639. This class provides enum for ISO639 code, but I need a type conversion on from ISO639 enum to plain string. (ex. ISO639.ISO639Code.Italian => "it"). I also need a type conversion from plain string to ISO639 enum. (ex. "it" => ISO639.ISO639Code.Italian).

Is there a more efficient coding style for that?

like image 602
mjk6026 Avatar asked Jul 26 '11 10:07

mjk6026


1 Answers

You can add standard System.ComponentModel.Description attribute to each enum entry and then read it.

public enum ISO639Code       
{ 
  [Description("af")]
  Afrikaans
}

public static class EnumExtensions
{
    // Extension method to read Description value
    public static string GetDescription(this Enum currentEnum)
    {
         var fi = currentEnum.GetType().GetField(currentEnum.ToString()); 
         var da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)); 
         return da != null ? da.Description : currentEnum.ToString();
     } 
}

// **How-to read it**
ISO639Code isoCode = ISO639Code.Afrikaans;

// this will returns "af"
string isoDescription = isoCode.GetDescription(); 

EDIT:

    string searchFor = "af"; 
    ISO639Code foundEntry;

    // Loop through all entries descriptions       
    var allEntries = Enum.GetValues(typeof(ISO639Code));
    foreach (var entry in allEntries)
    {
        // If you will extract this as separate method and use it for search not only loop
        // through the all entries - you can put here is yield return description
        var currentEntry = ((ISO639Code)entry);
        string description = currentEntry.GetDescription();
        if (description == searchFor)
        {
            foundEntry = currentEntry;
            break;
        }
    }
like image 87
sll Avatar answered Oct 24 '22 04:10

sll