如何国际化表示 C# 枚举值的字符串? [英] How can I internationalize strings representing C# enum values?

查看:36
本文介绍了如何国际化表示 C# 枚举值的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多关于将字符串映射到枚举以及反之亦然的问题和答案,但是如何将一系列本地化字符串映射到枚举?

I've seen many questions and answers about mapping strings to enums and vice-versa, but how can I map a series of localized strings to enums?

我是否应该创建一个 这样的扩展方法,从资源中返回正确的字符串文件?有没有办法本地化 这样的解决方案中使用的属性(如描述")?

Should I just create an extension method like this that returns the proper string from a resource file? Is there a way to localize attributes (like "Description") that are used in solutions like this?

这是首选解决方案 - 扩展方法或属性.在我看来,这不是属性的预期目的.事实上,现在我想起来了,如果我要使用扩展方法,属性似乎是我用来在资源文件中为我想用来代替枚举值的本地化字符串指定一个键的东西.

Which is the preferred solution - extension method or attributes. It seems to me that this isn't the intended purpose of attributes. In fact, now that I think about it, if I were to use an extension method an attribute seems like something I'd use to specify a key in a resource file for the localized string I want to use in place of the enum value.

编辑 - 示例:

鉴于以下枚举,

public enum TransactionTypes {
    Cheque = 1,
    BankTransfer = 2,
    CreditCard = 3
}

我想要一种将每种类型映射到本地化字符串的方法.我从枚举的扩展方法开始,它使用 switch 语句和对资源文件的强类型引用.

I would like a way to map each type to a localized string. I started off with an extension method for the enum that uses a switch statement and strongly typed references to the resource file.

但是,每个枚举的扩展方法似乎并不是一个很好的解决方案.我已经开始关注 this 为每个枚举值创建自定义属性.该属性具有包含本地化字符串的资源文件的基本名称和键.例如,在上面的枚举中,我有这个:

However, an extension method for every enum doesn't seem like a great solution. I've started following this to create a custom attribute for each enumerated value. The attribute has a base name and key for the resource file containing localized strings. In the above enum, for example, I have this:

...
[EnumResourceAttribute("FinancialTransaction", "Cheque")]
Cheque = 1,
...

其中FinanacialTransaction"是 resx 文件,Cheque"是字符串键.我正在尝试创建一个实用程序方法,我可以将任何枚举中的任何值传递给该方法,并让它返回该值的本地化字符串表示形式,假设指定了自定义属性.我只是不知道如何动态访问资源文件和其中的键.

Where "FinanacialTransaction" is the resx file and "Cheque" is the string key. I'm trying to create a utility method to which I could pass any value from any enumeration and have it return the localized string representation of that value, assuming the custom attribute is specified. I just can't figure out how to dynamically access a resource file and a key within it.

推荐答案

我继续使用自定义属性并创建了这个实用方法:

I continued with the custom attributes and created this utility method:

    public static string getEnumResourceString(Enum value)
    {
        System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString());
        EnumResourceAttribute attr = (EnumResourceAttribute)System.Attribute.GetCustomAttribute(fi, typeof(EnumResourceAttribute));
        return (string)HttpContext.GetGlobalResourceObject(attr.BaseName, attr.Key);
    }

这篇关于如何国际化表示 C# 枚举值的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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