Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Enum Display Extension

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
30 Oct 2011CPOL 23.4K   10   10
In response to Adrian Cole's comment to the OP - due to living and working in a country where there are four official languages, plus English as a de facto fifth, I've encountered this before.Provided you have the corresponding .resx files embedded in the same assembly as the enum and your...

In response to Adrian Cole's comment to the OP - due to living and working in a country where there are four official languages, plus English as a de facto fifth, I've encountered this before.


Provided you have the corresponding .resx files embedded in the same assembly as the enum and your enum values correspond to the resource names in those files, you can do the following:


Create a bog-standard enum without attributes:


C#
//standard enum
public enum EmployeeType
{
    Boss,
    AdministrativeAssistant,
    Janitor,
    StandardEmployee
}

And create an extension method that uses the enum value as the key for the .NET Framework to look up the appropriate language string:


C#
//Enum Extension class
public static class EnumExtensions
{
    //Gets the localized string representation of the enum value
    public static string ToLocalizedString(this Enum value)
    {
        var resourceManager = new ResourceManager(Type.GetType(
            string.Format("{0}.{1}Strings", 
            value.GetType().Namespace, value.GetType().Name)));

        var localizedString = resourceManager.GetString(value.ToString());

        return string.IsNullOrEmpty(localizedString) ? value.ToString() : localizedString;
    }
}

Then all you need to do to get a localized string in place of the enum value is:


C#
var localized = EmployeeType.Boss.ToLocalizedString();

And for converting a localized string back to the enum, you call an extension method on the string:


C#
//string extension method
public static T FromLocalizedString<T>(this string localizedString)
{
   var t = typeof (T);

   var resourceType = Type.GetType(string.Format("{0}.{1}Strings", 
                                   t.Namespace, t.Name));

   var enumObject = resourceType.GetProperties()
       .Where(p => p.GetValue(null, null) != null && 
              p.GetValue(null, null).ToString() == localizedString)
       .DefaultIfEmpty(null)
       .FirstOrDefault();

   return enumObject == null ? default(T) : (T)Enum.Parse(t, enumObject.Name);
}

like this:


C#
var boss = "Chef".FromLocalizedString<EmployeeType>();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
First batch file in 1987

Messed around with the Bullfrog C++ Libraries for Syndicate in 1994

Web Developer since 2000

.net Developer since 2001

MCTS: Microsoft® .NET Framework 2.0 - Web-based Client Development

MCTS: Web Applications Development with Microsoft .NET Framework 4

Comments and Discussions

 
GeneralReason for my vote of 5 5. End of story. Pin
Oshtri Deka29-Feb-12 21:08
professionalOshtri Deka29-Feb-12 21:08 
GeneralReason for my vote of 5 Another great implementation! Pin
FernandoUY21-Dec-11 14:01
professionalFernandoUY21-Dec-11 14:01 
GeneralReason for my vote of 5 Wonderful! Pin
Pablo Aliskevicius16-Nov-11 19:51
Pablo Aliskevicius16-Nov-11 19:51 
GeneralRe: A wonderful comment too! Thanks! Pin
jim lahey17-Nov-11 2:01
jim lahey17-Nov-11 2:01 
GeneralIf you use the DescriptionAttribute for the enums as part of... Pin
germ1331-Oct-11 22:26
germ1331-Oct-11 22:26 
GeneralRe: I have no idea. You'll have to ask the OP. Pin
jim lahey1-Nov-11 0:49
jim lahey1-Nov-11 0:49 
GeneralReason for my vote of 5 Very nice implementation. Pin
fjdiewornncalwe27-Oct-11 4:47
professionalfjdiewornncalwe27-Oct-11 4:47 
GeneralRe: Thankyou! Pin
jim lahey27-Oct-11 4:50
jim lahey27-Oct-11 4:50 
GeneralMessage Removed Pin
26-Oct-11 3:20
professionalN_tro_P26-Oct-11 3:20 
GeneralRe: Thanks! Pin
jim lahey26-Oct-11 3:50
jim lahey26-Oct-11 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.