public EnumValues(ClrType type) { Debug.Assert(type.IsEnum); enumNames = type.GetEnumNames().ToArray(); enumValues = new long[enumNames.Length]; enumKind = type.GetEnumElementType(); for (int i = 0, icnt = enumNames.Length; i < icnt; ++i) { object obj; if (type.TryGetEnumValue(enumNames[i], out obj)) { enumValues[i] = ConvertToLong(obj, enumKind); } else { enumValues[i] = InvalidEnumValue; } } }
static void ReadEnum(ClrType type, ulong obj) { int objValue = (int)type.GetValue(obj); bool found = false; foreach (var name in type.GetEnumNames()) { int value; if (type.TryGetEnumValue(name, out value) && objValue == value) { Console.WriteLine("{0} - {1}", value, name); found = true; break; } } if (!found) { Console.WriteLine("{0} - {1}", objValue, "Unknown"); } }
public static string GetEnumName(ClrType type, ulong enumValue) { if (type == null || !type.IsEnum) { return("#INVALIDENUMTYPE#"); } object value = enumValue; List <string> namesFound = new List <string>(); ClrElementType enumType = ClrElementType.Unknown; try { enumType = type.GetEnumElementType(); } catch { return("#INVALIDENUMTYPE#"); } byte[] raw = BitConverter.GetBytes(enumValue); object gvalue = enumValue; switch (enumType) { case ClrElementType.Int16: gvalue = BitConverter.ToInt16(raw, 0); break; case ClrElementType.Int32: gvalue = BitConverter.ToInt32(raw, 0); break; case ClrElementType.Int64: gvalue = BitConverter.ToInt64(raw, 0); break; case ClrElementType.UInt16: gvalue = BitConverter.ToUInt16(raw, 0); break; case ClrElementType.UInt32: gvalue = BitConverter.ToUInt32(raw, 0); break; case ClrElementType.UInt64: gvalue = BitConverter.ToUInt64(raw, 0); break; default: gvalue = 0UL; break; } if (!String.IsNullOrEmpty(type.GetEnumName(gvalue))) { return(type.GetEnumName(gvalue)); } var names = type.GetEnumNames(); foreach (var name in names) { if (type.TryGetEnumValue(name, out value)) { byte[] rawValue = null; switch (enumType) { case ClrElementType.Int16: rawValue = BitConverter.GetBytes((short)value); break; case ClrElementType.Int32: rawValue = BitConverter.GetBytes((int)value); break; case ClrElementType.Int64: rawValue = BitConverter.GetBytes((long)value); break; case ClrElementType.UInt16: rawValue = BitConverter.GetBytes((ushort)value); break; case ClrElementType.UInt32: rawValue = BitConverter.GetBytes((uint)value); break; case ClrElementType.UInt64: rawValue = BitConverter.GetBytes((ulong)value); break; default: rawValue = BitConverter.GetBytes((ulong)0UL); break; } byte[] raw64 = BitConverter.GetBytes((ulong)0UL); for (int i = 0; i < rawValue.Length; i++) { raw64[i] = rawValue[i]; } ulong u = BitConverter.ToUInt64(raw64, 0); if ((enumValue & u) == u && u != 0) { namesFound.Add(name); } } } if (namesFound.Count == 0) { return(String.Empty); } return(String.Join("|", namesFound.ToArray())); }
/// <summary> /// Returns a list of names in the enum. /// </summary> /// <returns>IEnumerable<System.String>.</returns> public IEnumerable <string> GetEnumNames() => ClrType.GetEnumNames();
public void GetEnumNames(out IMDStringEnum ppEnum) { ppEnum = new MDStringEnum(m_type.GetEnumNames().ToArray()); }