Resource on an Enum

Asked

Viewed 607 times

7

I have a enum and would like to change the Description with Resource:

public enum SystemArea
{
    [Description("Gestor")]
    Gestor = 3,
    [Description("Administrador")]
    Administrador = 2,
    [Description("Professor | Profesor")]
    Professor = 1,
    [Description("Aluno | Alumno")]
    Aluno = 0,
}

I’ve tried it in the following ways, but none of it worked: 1) Passing the Resource to Description itself

[Description(Resources.DISPLAY_GESTOR)]
Gestor = 3

2) With the Display:

[Display(Name = "DISPLAY_GESTOR", ResourceType = typeof(Resources))]
Gestor = 3

Is there any way to solve?

  • 1

    Look at this, it helps you more than the answer I posted? http://stackoverflow.com/questions/569298/localizing-enum-descriptions-attributes if applicable, I will update the response. You also have: http://stackoverflow.com/q/17380900/221800

2 answers

5

One way to solve is to create an extension method, which if there is no Resource, returns the Enum item itself:

public static class SystemAreaExtension
{
  public static string Display(this SystemArea value) {
    var compare = new ResourceManager("SystemArea", Assembly.GetExecutingAssembly())
                    .GetString("SystemArea_" + value);
    return string.IsNullOrEmpty(compare) ? value : compare;
  }
}

Or generic:

public static class EnumExtension {
  public static string Display(this Enum item) {
    var compare = new ResourceManager("SystemArea", Assembly.GetExecutingAssembly())
                  .GetString(item.GetType().Name + "_" + item);
    return string.IsNullOrEmpty(compare) ? item.ToString() : compare;
  }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Obviously resources need to be put this way. But it can be adapted to what you need.

  • I think only the second would answer well. + 1.

  • It’s the one I would use. But there are people who might want to do something different depending on the type.

5


I tried to get the best of both worlds:

So I was able to solve the problem as follows:
From licking a static method to return the Resource of all items.

namespace SeuNamespace
{
    public class Enumeration
    {
        public enum MeuEnum
        {
            [LocalizedEnum("DISPLAY_ITEM1", ResourceType = typeof(Resources))]
            Item1 = 4,
            [LocalizedEnum("DISPLAY_ITEM2", ResourceType = typeof(Resources))]
            Item2 = 3,
            [LocalizedEnum("DISPLAY_ITEM3", ResourceType = typeof(Resources))]
            Item3 = 2
        }

        public static List<string> GetAllEnumDescription()
        {
            List<string> resultado = new List<string>();

            foreach (MeuEnum value in Enum.GetValues(typeof(MeuEnum)))
            {
                FieldInfo fi = value.GetType().GetField(value.ToString());

                LocalizedEnumAttribute[] attributes =
                    (LocalizedEnumAttribute[])fi.GetCustomAttributes(typeof(LocalizedEnumAttribute), false);

                if (attributes != null && attributes.Length > 0)
                    resultado.Add(value.GetLocalizedDescription());
                else
                    resultado.Add(value.ToString());
            }

            return resultado;
        }
    }

    #region EnumAttribute
    public class LocalizedEnumAttribute : DescriptionAttribute
    {
        private PropertyInfo _nameProperty;
        private Type _resourceType;

        public LocalizedEnumAttribute(string displayNameKey)
            : base(displayNameKey)
        {

        }

        public Type ResourceType
        {
            get
            {
                return _resourceType;
            }
            set
            {
                _resourceType = value;

                _nameProperty = _resourceType.GetProperty(this.Description, BindingFlags.Static | BindingFlags.Public);
            }
        }

        public override string Description
        {
            get
            {
                if (_nameProperty == null)
                {
                    return base.Description;
                }

                return (string)_nameProperty.GetValue(_nameProperty.DeclaringType, null);
            }
        }
    }

    public static class EnumExtender
    {
        public static string GetLocalizedDescription(this Enum @enum)
        {
            if (@enum == null)
                return null;

            string description = @enum.ToString();

            FieldInfo fieldInfo = @enum.GetType().GetField(description);
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes.Any())
                return attributes[0].Description;

            return description;
        }
    }

    #endregion
}


To get the Resource of only one item is enough: MeuEnum.Item1.GetLocalizedDescription();.
To get the re-source of all items just need: MeuEnum.GetAllEnumDescription();.

Tested and approved. Be happy!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.