微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在.Net中有没有办法获得int字的字符串值?

例如:
(1).someFunction().Equals("one")
(2).someFunction().Equals("two")

在我正在使用的情况下,我真的只需要数字1-9,我应该只使用一个开关/选择案例吗?

更新我也不需要本地化.

更新2这是我最终使用的内容

Private Enum EnglishDigit As Integer
    zero
    one
    two
    three
    four
    five
    six
    seven
    eight
    nine
End Enum

(CType(someIntThatIsLessthanTen,EnglishDigit)).ToString()
枚举怎么样?
enum Number
{
    One = 1,// default value for first enum element is 0,so we set = 1 here
    Two,Three,Four,Five,Six,Seven,Eight,Nine,}

然后你可以输入像…这样的东西

((Number)1).ToString()

如果您需要本地化,则可以为每个枚举值添加DescriptionAttribute.属性的Description属性将存储资源项的密钥的名称.

enum Number
{
    [Description("NumberName_1")]
    One = 1,so we set = 1 here 

    [Description("NumberName_2")]
    Two,// and so on...
}

以下函数将从属性获取Description属性的值

public static string GetDescription(object value)
{
    DescriptionAttribute[] attributes = null;
    System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString());
    if (fi != null)
    {
        attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),false);
    }

    string description = null;
    if ((attributes != null) && (attributes.Length > 0))
    {
        description = attributes[0].Description;
    }

    return description;
}

这可以通过以下方式调用

GetDescription(((Number)1))

然后,您可以从资源文件提取相关值,或者如果返回null则只调用.ToString().

编辑

各种评论者指出(我必须同意)只使用枚举值名称来引用本地化字符串会更简单.

原文地址:https://www.jb51.cc/vb/255344.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐