Ejemplo n.º 1
0
        private void InitEnumData()
        {
            if (!IsEnum)
            {
                throw new InvalidOperationException("Type is not an Enum.");
            }

            _enumData = new EnumData();
            MetaDataImport import = DesktopModule?.GetMetadataImport();

            if (import == null)
            {
                return;
            }

            IntPtr        hnd   = IntPtr.Zero;
            List <string> names = new List <string>();

            foreach (int token in import.EnumerateFields((int)_token))
            {
                if (import.GetFieldProps(token, out string name, out FieldAttributes attr, out IntPtr ppvSigBlob, out int pcbSigBlob, out int pdwCPlusTypeFlag, out IntPtr ppValue))
                {
                    if ((int)attr == 0x606 && name == "value__")
                    {
                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);

                        if (parser.GetCallingConvInfo(out int sigType) && parser.GetElemType(out int elemType))
                        {
                            _enumData.ElementType = (ClrElementType)elemType;
                        }
                    }

                    // public, static, literal, has default
                    int intAttr = (int)attr;
                    if ((int)attr == 0x8056)
                    {
                        names.Add(name);

                        SigParser parser = new SigParser(ppvSigBlob, pcbSigBlob);
                        parser.GetCallingConvInfo(out int ccinfo);
                        parser.GetElemType(out int elemType);

                        Type type = ClrRuntime.GetTypeForElementType((ClrElementType)pdwCPlusTypeFlag);
                        if (type != null)
                        {
                            object o = System.Runtime.InteropServices.Marshal.PtrToStructure(ppValue, type);
                            _enumData.NameToValue[name] = o;
                            _enumData.ValueToName[o]    = name;
                        }
                    }
                }
            }
        }