Click here to Skip to main content
11,491,598 members (70,717 online)
Click here to Skip to main content

Tagged as

Enum Display Extension

, 30 Oct 2011 CPOL 7.8K 10
Rate this:
Please Sign up or sign in to vote.
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:

//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:

//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:

var localized = EmployeeType.Boss.ToLocalizedString();

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

//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:

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)

Share

About the Author

jim lahey
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 22:08
memberOshtri Deka29-Feb-12 22:08 
GeneralReason for my vote of 5 Another great implementation! Pin
FernandaUY21-Dec-11 15:01
memberFernandaUY21-Dec-11 15:01 
GeneralReason for my vote of 5 Wonderful! Pin
Pablo Aliskevicius16-Nov-11 20:51
memberPablo Aliskevicius16-Nov-11 20:51 
GeneralRe: A wonderful comment too! Thanks! Pin
jim lahey17-Nov-11 3:01
memberjim lahey17-Nov-11 3:01 
GeneralIf you use the DescriptionAttribute for the enums as part of... Pin
germ1331-Oct-11 23:26
membergerm1331-Oct-11 23:26 
GeneralRe: I have no idea. You'll have to ask the OP. Pin
jim lahey1-Nov-11 1:49
memberjim lahey1-Nov-11 1:49 
GeneralReason for my vote of 5 Very nice implementation. Pin
Marcus Kramer27-Oct-11 5:47
memberMarcus Kramer27-Oct-11 5:47 
GeneralRe: Thankyou! Pin
jim lahey27-Oct-11 5:50
memberjim lahey27-Oct-11 5:50 
GeneralNice modification to it :-) Pin
Collin Jasnoch26-Oct-11 4:20
memberCollin Jasnoch26-Oct-11 4:20 
GeneralRe: Thanks! Pin
jim lahey26-Oct-11 4:50
memberjim lahey26-Oct-11 4:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 30 Oct 2011
Article Copyright 2011 by jim lahey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid