Ejemplo n.º 1
0
 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;
         }
     }
 }
Ejemplo n.º 2
0
        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");
            }
        }
Ejemplo n.º 3
0
        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()));
        }
Ejemplo n.º 4
0
 /// <summary>
 ///     Returns a list of names in the enum.
 /// </summary>
 /// <returns>IEnumerable&lt;System.String&gt;.</returns>
 public IEnumerable <string> GetEnumNames() => ClrType.GetEnumNames();
Ejemplo n.º 5
0
 public void GetEnumNames(out IMDStringEnum ppEnum)
 {
     ppEnum = new MDStringEnum(m_type.GetEnumNames().ToArray());
 }