Resource in an enum

7

I have 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 the following ways, but none worked: 1) Passing the Resource into the Description itself

[Description(Resources.DISPLAY_GESTOR)]
Gestor = 3

2) With Display:

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

Do you have any way to solve it?

    
asked by anonymous 27.08.2015 / 19:46

2 answers

5

I tried to catch the best of both worlds:

So I solved the problem as follows:
From lambuja 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, just MeuEnum.Item1.GetLocalizedDescription(); . To get the resource of all items just: MeuEnum.GetAllEnumDescription(); .

Tested and approved. Be happy!

    
07.10.2015 / 16:42
5

One way to solve is to create an extension method, which if there is no resource, return 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;
  }
}

See more or less how it would work in dotNetFiddle .

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

    
27.08.2015 / 19:56