public override EnumInfo GetEnumInfo(Type type)
 {
     return(new EnumInfo(
                RuntimeAugments.GetEnumUnderlyingType(type.TypeHandle),
                rawValues: Array.Empty <object>(),
                names: Array.Empty <string>(),
                isFlags: false));
 }
Ejemplo n.º 2
0
        public static EnumInfo Create(RuntimeTypeHandle typeHandle, MetadataReader reader, TypeDefinitionHandle typeDefHandle)
        {
            TypeDefinition typeDef = reader.GetTypeDefinition(typeDefHandle);

            // Count the number of static fields. The single instance field may or may not have metadata,
            // so using `typeDef.Fields.Count - 1` is not reliable.
            int staticFieldCount = 0;

            foreach (FieldHandle fieldHandle in typeDef.Fields)
            {
                Field field = fieldHandle.GetField(reader);
                if (0 != (field.Flags & FieldAttributes.Static))
                {
                    staticFieldCount++;
                }
            }

            string[] names  = new string[staticFieldCount];
            object[] values = new object[staticFieldCount];

            int i = 0;

            foreach (FieldHandle fieldHandle in typeDef.Fields)
            {
                Field field = fieldHandle.GetField(reader);
                if (0 != (field.Flags & FieldAttributes.Static))
                {
                    names[i]  = field.Name.GetString(reader);
                    values[i] = field.DefaultValue.ParseConstantNumericValue(reader);
                    i++;
                }
            }

            bool isFlags = false;

            foreach (CustomAttributeHandle cah in typeDef.CustomAttributes)
            {
                if (cah.IsCustomAttributeOfType(reader, "System", "FlagsAttribute"))
                {
                    isFlags = true;
                }
            }

            return(new EnumInfo(RuntimeAugments.GetEnumUnderlyingType(typeHandle), values, names, isFlags));
        }
Ejemplo n.º 3
0
        public static EnumInfo Create(RuntimeTypeHandle typeHandle, MetadataReader reader, TypeDefinitionHandle typeDefHandle)
        {
            TypeDefinition typeDef = reader.GetTypeDefinition(typeDefHandle);

            // Per the spec, Enums are required to have one instance field. The rest are statics.
            int staticFieldCount = typeDef.GetFields().Count - 1;

            string[] names  = new string[staticFieldCount];
            object[] values = new object[staticFieldCount];

            int i = 0;

            foreach (FieldDefinitionHandle fieldHandle in typeDef.GetFields())
            {
                FieldDefinition field = reader.GetFieldDefinition(fieldHandle);
                if (0 != (field.Attributes & FieldAttributes.Static))
                {
                    if (i >= staticFieldCount || (field.Attributes & FieldAttributes.HasDefault) != FieldAttributes.HasDefault)
                    {
                        throw new BadImageFormatException();
                    }

                    names[i]  = reader.GetString(field.Name);
                    values[i] = field.GetDefaultValue().ParseConstantValue(reader);
                    i++;
                }
            }

            bool isFlags = false;

            foreach (CustomAttributeHandle cah in typeDef.GetCustomAttributes())
            {
                if (cah.IsCustomAttributeOfType(reader, "System", "FlagsAttribute"))
                {
                    isFlags = true;
                }
            }

            return(new EnumInfo(RuntimeAugments.GetEnumUnderlyingType(typeHandle), values, names, isFlags));
        }
        public sealed override EnumInfo GetEnumInfo(RuntimeTypeHandle typeHandle)
        {
            // Handle the weird case of an enum type nested under a generic type that makes the
            // enum itself generic
            RuntimeTypeHandle typeDefHandle = typeHandle;

            if (RuntimeAugments.IsGenericType(typeHandle))
            {
                typeDefHandle = RuntimeAugments.GetGenericDefinition(typeHandle);
            }

            // If the type is reflection blocked, we pretend there are no enum values defined
            if (ReflectionExecution.ExecutionEnvironment.IsReflectionBlocked(typeDefHandle))
            {
                return(new EnumInfo(RuntimeAugments.GetEnumUnderlyingType(typeHandle), Array.Empty <object>(), Array.Empty <string>(), false));
            }

            QTypeDefinition qTypeDefinition;

            if (!ReflectionExecution.ExecutionEnvironment.TryGetMetadataForNamedType(typeDefHandle, out qTypeDefinition))
            {
                throw ReflectionCoreExecution.ExecutionDomain.CreateMissingMetadataException(Type.GetTypeFromHandle(typeDefHandle));
            }

            if (qTypeDefinition.IsNativeFormatMetadataBased)
            {
                return(NativeFormatEnumInfo.Create(typeHandle, qTypeDefinition.NativeFormatReader, qTypeDefinition.NativeFormatHandle));
            }
#if ECMA_METADATA_SUPPORT
            if (qTypeDefinition.IsEcmaFormatMetadataBased)
            {
                return(EcmaFormatEnumInfo.Create(typeHandle, qTypeDefinition.EcmaFormatReader, qTypeDefinition.EcmaFormatHandle));
            }
#endif
            return(null);
        }