PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/corlib/System/Enum.cs

https://bitbucket.org/cosi2/dotnetanywhere-wb
C# | 114 lines | 95 code | 17 blank | 2 comment | 27 complexity | 6c7f2d22ff14ecfc0171a3e3f96bc35d MD5 | raw file
  1. #if !LOCALTEST
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace System {
  7. public abstract class Enum : ValueType {
  8. [MethodImpl(MethodImplOptions.InternalCall)]
  9. extern static private void Internal_GetInfo(Type enumType, out string[] names, out int[] values);
  10. private static Dictionary<Type, EnumInfo> cache = new Dictionary<Type, EnumInfo>();
  11. public static string[] GetNames(Type enumType) {
  12. if (enumType == null) {
  13. throw new ArgumentNullException();
  14. }
  15. if (!enumType.IsEnum) {
  16. throw new ArgumentException();
  17. }
  18. EnumInfo info = EnumInfo.GetInfo(enumType);
  19. return info.GetNames();
  20. }
  21. private struct EnumInfo {
  22. private string[] names;
  23. private int[] values;
  24. public static EnumInfo GetInfo(Type enumType) {
  25. lock (cache) {
  26. EnumInfo info;
  27. if (!Enum.cache.TryGetValue(enumType, out info)) {
  28. info = new EnumInfo();
  29. Enum.Internal_GetInfo(enumType, out info.names, out info.values);
  30. Enum.cache.Add(enumType, info);
  31. }
  32. return info;
  33. }
  34. }
  35. public string GetName(int value) {
  36. int valuesLen = values.Length;
  37. for (int i = 0; i < valuesLen; i++) {
  38. if (this.values[i] == value) {
  39. return this.names[i];
  40. }
  41. }
  42. // Pretend it's got the [Flags] attribute, so look for bits set.
  43. // TODO Sort out Flags attribute properly
  44. StringBuilder sb = new StringBuilder();
  45. for (int i = 0; i < valuesLen; i++) {
  46. int thisValue = this.values[i];
  47. if ((value & thisValue) == thisValue) {
  48. sb.Append(this.names[i]);
  49. sb.Append(", ");
  50. }
  51. }
  52. if (sb.Length > 0) {
  53. return sb.ToString(0, sb.Length - 2);
  54. }
  55. return null;
  56. }
  57. public string[] GetNames() {
  58. List<string> names = new List<string>();
  59. for (int i = 0; i < this.values.Length; i++) {
  60. names.Add(this.GetName(this.values[i]));
  61. }
  62. return names.ToArray();
  63. }
  64. }
  65. protected Enum() { }
  66. [MethodImpl(MethodImplOptions.InternalCall)]
  67. extern private int Internal_GetValue();
  68. public static string GetName(Type enumType, object value) {
  69. if (enumType == null || value == null) {
  70. throw new ArgumentNullException();
  71. }
  72. if (!enumType.IsEnum) {
  73. throw new ArgumentException("enumType is not an Enum type.");
  74. }
  75. EnumInfo info = EnumInfo.GetInfo(enumType);
  76. return info.GetName((int)value);
  77. }
  78. public static string Format(Type enumType, object value, string format) {
  79. if (enumType == null || value == null || format == null) {
  80. throw new ArgumentNullException("enumType");
  81. }
  82. if (!enumType.IsEnum) {
  83. throw new ArgumentException("Type provided must be an Enum.");
  84. }
  85. string ret = GetName(enumType, value);
  86. if (ret == null) {
  87. return value.ToString();
  88. } else {
  89. return ret;
  90. }
  91. }
  92. public override string ToString() {
  93. return Format(this.GetType(), this.Internal_GetValue(), "G");
  94. }
  95. }
  96. }
  97. #endif