链接枚举值与本地化的字符串资源 [英] Linking enum value with localized string resource

查看:144
本文介绍了链接枚举值与本地化的字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关报道: 演员字符串与枚举属性枚举

我想结合枚举的最易维护的方式和它相关的本地化字符串值的东西。

如果我坚持枚举和类在同一个文件,我觉得有点安全,但我必须假定有一个更好的办法。我也认为具有枚举名称相同的资源串的名字,但我怕我不能总是在这里强制执行。

 使用CR = AcmeCorp.Properties.Resources;

公共枚举SourceFilterOption
{
    LastNumberOccurences,
    LastNumberWeeks,
    日期范围
    //如果添加到这一点,你必须更新FilterOptions.GetString
}

公共类FilterOptions
{
    公共字典< SourceFilterOption,字符串> GetEnumWithResourceString()
    {
        VAR字典=新字典< SourceFilterOption,字符串>();
        的foreach(在Enum.GetValues​​ SourceFilterOption过滤器(typeof运算(SourceFilterOption)))
        {
            dict.Add(过滤器,GetString的(过滤器));
        }
        返回字典;
    }

    公共字符串的GetString(SourceFilterOption选项)
    {
        开关(选配件)
        {
            案例SourceFilterOption.LastNumberOccurences:
                返回CR.LAST_NUMBER_OF_OCCURANCES;
            案例SourceFilterOption.LastNumberWeeks:
                返回CR.LAST_NUMBER_OF_WEEKS;
            案例SourceFilterOption.DateRange:
            默认:
                返回CR.DATE_RANGE;
        }
    }
}
 

解决方案

您可以添加DescriptionAttribute到每个枚举值。

 公开枚举SourceFilterOption
{
    [描述(LAST_NUMBER_OF_OCCURANCES)
    LastNumberOccurences,
    ...
}
 

拉​​出描述(资源键),当你需要它。

 字段信息网络= value.GetType()getfield命令(value.ToString())。
DescriptionAttribute []属性=(DescriptionAttribute [])fi.GetCustomAttributes(typeof运算(DescriptionAttribute)
如果(attributes.Length大于0)
{
    返回属性[0]。说明;
}
其他
{
    返回value.ToString();
}
 

<一个href="http://geekswithblogs.net/paulwhitblog/archive/2008/03/31/use-the-descriptionattribute-with-an-enum-to-display-status-messages.aspx">http://geekswithblogs.net/paulwhitblog/archive/2008/03/31/use-the-descriptionattribute-with-an-enum-to-display-status-messages.aspx

编辑:回应评论(@Tergiver)。使用(现有的)DescriptionAttribute在我的例子就是要迅速完成任务。你会得到更好的实现自己的自定义属性,而不是使用它的目的,一组在外面。事情是这样的:

  [AttributeUsage(AttributeTargets.Field,的AllowMultiple =假的,可继承= FALSE)]
公共类EnumResourceKeyAttribute:属性
{
 公共字符串的ResourceKey {获得;组; }
}
 

Related: Cast string to enum with enum attribute

I want the most maintainable way of binding an enumeration and it's associated localized string values to something.

If I stick the enum and the class in the same file I feel somewhat safe but I have to assume there is a better way. I've also considered having the enum name be the same as the resource string name, but I'm afraid I can't always be here to enforce that.

using CR = AcmeCorp.Properties.Resources;

public enum SourceFilterOption
{
    LastNumberOccurences,
    LastNumberWeeks,
    DateRange
    // if you add to this you must update FilterOptions.GetString
}

public class FilterOptions
{
    public Dictionary<SourceFilterOption, String> GetEnumWithResourceString()
    {
        var dict = new Dictionary<SourceFilterOption, String>();
        foreach (SourceFilterOption filter in Enum.GetValues(typeof(SourceFilterOption)))
        {
            dict.Add(filter, GetString(filter));
        }
        return dict;
    }

    public String GetString(SourceFilterOption option)
    {
        switch (option)
        {
            case SourceFilterOption.LastNumberOccurences:
                return CR.LAST_NUMBER_OF_OCCURANCES;
            case SourceFilterOption.LastNumberWeeks:
                return CR.LAST_NUMBER_OF_WEEKS;
            case SourceFilterOption.DateRange:
            default:
                return CR.DATE_RANGE;
        }
    }
}

解决方案

You can add DescriptionAttribute to each enum value.

public enum SourceFilterOption
{
    [Description("LAST_NUMBER_OF_OCCURANCES")]
    LastNumberOccurences,
    ...
}

Pull out the description (resource key) when you need it.

FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),     
if (attributes.Length > 0)
{
    return attributes[0].Description;
}
else
{
    return value.ToString();
}

http://geekswithblogs.net/paulwhitblog/archive/2008/03/31/use-the-descriptionattribute-with-an-enum-to-display-status-messages.aspx

Edit: Response to comments (@Tergiver). Using the (existing) DescriptionAttribute in my example is to get the job done quickly. You would be better implementing your own custom attribute instead of using one outside of its purpose. Something like this:

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inheritable = false)]
public class EnumResourceKeyAttribute : Attribute
{
 public string ResourceKey { get; set; }
}

这篇关于链接枚举值与本地化的字符串资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆