dotnetanywhere-wb /corlib/System/Enum.cs

Language C# Lines 115
MD5 Hash 6c7f2d22ff14ecfc0171a3e3f96bc35d Estimated Cost $2,282 (why?)
Repository https://bitbucket.org/cosi2/dotnetanywhere-wb.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#if !LOCALTEST

using System;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Text;

namespace System {
	public abstract class Enum : ValueType {

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern static private void Internal_GetInfo(Type enumType, out string[] names, out int[] values);

		private static Dictionary<Type, EnumInfo> cache = new Dictionary<Type, EnumInfo>();

		public static string[] GetNames(Type enumType) {
			if (enumType == null) {
				throw new ArgumentNullException();
			}
			if (!enumType.IsEnum) {
				throw new ArgumentException();
			}
			EnumInfo info = EnumInfo.GetInfo(enumType);
			return info.GetNames();
		}

		private struct EnumInfo {
			private string[] names;
			private int[] values;
			
			public static EnumInfo GetInfo(Type enumType) {
				lock (cache) {
					EnumInfo info;
					if (!Enum.cache.TryGetValue(enumType, out info)) {
						info = new EnumInfo();
						Enum.Internal_GetInfo(enumType, out info.names, out info.values);
						Enum.cache.Add(enumType, info);
					}
					return info;
				}
			}

			public string GetName(int value) {
				int valuesLen = values.Length;
				for (int i = 0; i < valuesLen; i++) {
					if (this.values[i] == value) {
						return this.names[i];
					}
				}
				// Pretend it's got the [Flags] attribute, so look for bits set.
				// TODO Sort out Flags attribute properly
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < valuesLen; i++) {
					int thisValue = this.values[i];
					if ((value & thisValue) == thisValue) {
						sb.Append(this.names[i]);
						sb.Append(", ");
					}
				}
				if (sb.Length > 0) {
					return sb.ToString(0, sb.Length - 2);
				}
				return null;
			}

			public string[] GetNames() {
				List<string> names = new List<string>();
				for (int i = 0; i < this.values.Length; i++) {
					names.Add(this.GetName(this.values[i]));
				}
				return names.ToArray();
			}

		}

		protected Enum() { }

		[MethodImpl(MethodImplOptions.InternalCall)]
		extern private int Internal_GetValue();

		public static string GetName(Type enumType, object value) {
			if (enumType == null || value == null) {
				throw new ArgumentNullException();
			}
			if (!enumType.IsEnum) {
				throw new ArgumentException("enumType is not an Enum type.");
			}
			EnumInfo info = EnumInfo.GetInfo(enumType);
			return info.GetName((int)value);
		}

		public static string Format(Type enumType, object value, string format) {
			if (enumType == null || value == null || format == null) {
				throw new ArgumentNullException("enumType");
			}
			if (!enumType.IsEnum) {
				throw new ArgumentException("Type provided must be an Enum.");
			}
			string ret = GetName(enumType, value);
			if (ret == null) {
				return value.ToString();
			} else {
				return ret;
			}
		}

		public override string ToString() {
			return Format(this.GetType(), this.Internal_GetValue(), "G");
		}

	}
}

#endif
Back to Top