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

前端之家收集整理的这篇文章主要介绍了在.Net中有没有办法获得int字的字符串值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
例如:
  1. (1).SomeFunction().Equals("one")
  2. (2).SomeFunction().Equals("two")

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

更新我也不需要本地化.

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

  1. Private Enum EnglishDigit As Integer
  2. zero
  3. one
  4. two
  5. three
  6. four
  7. five
  8. six
  9. seven
  10. eight
  11. nine
  12. End Enum
  13.  
  14. (CType(someIntThatIsLessThanTen,EnglishDigit)).ToString()
枚举怎么样?
  1. enum Number
  2. {
  3. One = 1,// default value for first enum element is 0,so we set = 1 here
  4. Two,Three,Four,Five,Six,Seven,Eight,Nine,}

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

  1. ((Number)1).ToString()

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

  1. enum Number
  2. {
  3. [Description("NumberName_1")]
  4. One = 1,so we set = 1 here
  5.  
  6. [Description("NumberName_2")]
  7. Two,// and so on...
  8. }

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

  1. public static string GetDescription(object value)
  2. {
  3. DescriptionAttribute[] attributes = null;
  4. System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString());
  5. if (fi != null)
  6. {
  7. attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),false);
  8. }
  9.  
  10. string description = null;
  11. if ((attributes != null) && (attributes.Length > 0))
  12. {
  13. description = attributes[0].Description;
  14. }
  15.  
  16. return description;
  17. }

这可以通过以下方式调用

  1. GetDescription(((Number)1))

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

编辑

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

猜你在找的VB相关文章