Esempio n. 1
0
        private static Exception ParseBoxedEnumConstantValue(this ConstantBoxedEnumValueHandle handle, ReflectionDomain reflectionDomain, MetadataReader reader, out Object value)
        {
            if (!(reflectionDomain is Internal.Reflection.Core.Execution.ExecutionDomain))
            {
                throw new PlatformNotSupportedException(); // Cannot work because boxing enums won't work in non-execution domains.
            }
            ConstantBoxedEnumValue record = handle.GetConstantBoxedEnumValue(reader);

            Exception exception = null;
            Type      enumType  = reflectionDomain.TryResolve(reader, record.Type, new TypeContext(null, null), ref exception);

            if (enumType == null)
            {
                value = null;
                return(exception);
            }

            if (!enumType.GetTypeInfo().IsEnum)
            {
                throw new BadImageFormatException();
            }

            Type underlyingType = Enum.GetUnderlyingType(enumType);

            // Now box the value as the specified enum type.
            unsafe
            {
                switch (record.Value.HandleType)
                {
                case HandleType.ConstantByteValue:
                {
                    if (underlyingType != typeof(byte))
                    {
                        throw new BadImageFormatException();
                    }

                    byte v = record.Value.ToConstantByteValueHandle(reader).GetConstantByteValue(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantSByteValue:
                {
                    if (underlyingType != typeof(sbyte))
                    {
                        throw new BadImageFormatException();
                    }

                    sbyte v = record.Value.ToConstantSByteValueHandle(reader).GetConstantSByteValue(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantInt16Value:
                {
                    if (underlyingType != typeof(short))
                    {
                        throw new BadImageFormatException();
                    }

                    short v = record.Value.ToConstantInt16ValueHandle(reader).GetConstantInt16Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantUInt16Value:
                {
                    if (underlyingType != typeof(ushort))
                    {
                        throw new BadImageFormatException();
                    }

                    ushort v = record.Value.ToConstantUInt16ValueHandle(reader).GetConstantUInt16Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantInt32Value:
                {
                    if (underlyingType != typeof(int))
                    {
                        throw new BadImageFormatException();
                    }

                    int v = record.Value.ToConstantInt32ValueHandle(reader).GetConstantInt32Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantUInt32Value:
                {
                    if (underlyingType != typeof(uint))
                    {
                        throw new BadImageFormatException();
                    }

                    uint v = record.Value.ToConstantUInt32ValueHandle(reader).GetConstantUInt32Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantInt64Value:
                {
                    if (underlyingType != typeof(long))
                    {
                        throw new BadImageFormatException();
                    }

                    long v = record.Value.ToConstantInt64ValueHandle(reader).GetConstantInt64Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantUInt64Value:
                {
                    if (underlyingType != typeof(ulong))
                    {
                        throw new BadImageFormatException();
                    }

                    ulong v = record.Value.ToConstantUInt64ValueHandle(reader).GetConstantUInt64Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                default:
                    throw new BadImageFormatException();
                }
            }
        }
        private static Exception ParseBoxedEnumConstantValue(this ConstantBoxedEnumValueHandle handle, MetadataReader reader, out Object value)
        {
            ConstantBoxedEnumValue record = handle.GetConstantBoxedEnumValue(reader);

            Exception exception = null;
            Type      enumType  = record.Type.TryResolve(reader, new TypeContext(null, null), ref exception);

            if (enumType == null)
            {
                value = null;
                return(exception);
            }

            if (!enumType.IsEnum)
            {
                throw new BadImageFormatException();
            }

            Type underlyingType = Enum.GetUnderlyingType(enumType);

            // Now box the value as the specified enum type.
            unsafe
            {
                switch (record.Value.HandleType)
                {
                case HandleType.ConstantByteValue:
                {
                    if (underlyingType != CommonRuntimeTypes.Byte)
                    {
                        throw new BadImageFormatException();
                    }

                    byte v = record.Value.ToConstantByteValueHandle(reader).GetConstantByteValue(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantSByteValue:
                {
                    if (underlyingType != CommonRuntimeTypes.SByte)
                    {
                        throw new BadImageFormatException();
                    }

                    sbyte v = record.Value.ToConstantSByteValueHandle(reader).GetConstantSByteValue(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantInt16Value:
                {
                    if (underlyingType != CommonRuntimeTypes.Int16)
                    {
                        throw new BadImageFormatException();
                    }

                    short v = record.Value.ToConstantInt16ValueHandle(reader).GetConstantInt16Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantUInt16Value:
                {
                    if (underlyingType != CommonRuntimeTypes.UInt16)
                    {
                        throw new BadImageFormatException();
                    }

                    ushort v = record.Value.ToConstantUInt16ValueHandle(reader).GetConstantUInt16Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantInt32Value:
                {
                    if (underlyingType != CommonRuntimeTypes.Int32)
                    {
                        throw new BadImageFormatException();
                    }

                    int v = record.Value.ToConstantInt32ValueHandle(reader).GetConstantInt32Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantUInt32Value:
                {
                    if (underlyingType != CommonRuntimeTypes.UInt32)
                    {
                        throw new BadImageFormatException();
                    }

                    uint v = record.Value.ToConstantUInt32ValueHandle(reader).GetConstantUInt32Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantInt64Value:
                {
                    if (underlyingType != CommonRuntimeTypes.Int64)
                    {
                        throw new BadImageFormatException();
                    }

                    long v = record.Value.ToConstantInt64ValueHandle(reader).GetConstantInt64Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                case HandleType.ConstantUInt64Value:
                {
                    if (underlyingType != CommonRuntimeTypes.UInt64)
                    {
                        throw new BadImageFormatException();
                    }

                    ulong v = record.Value.ToConstantUInt64ValueHandle(reader).GetConstantUInt64Value(reader).Value;
                    value = RuntimeAugments.Box(enumType.TypeHandle, (IntPtr)(&v));
                    return(null);
                }

                default:
                    throw new BadImageFormatException();
                }
            }
        }
Esempio n. 3
0
        private MetadataRecord HandleCustomAttributeConstantArray(
            Cts.ArrayType type, ImmutableArray <Ecma.CustomAttributeTypedArgument <Cts.TypeDesc> > value)
        {
            Cts.TypeDesc elementType = type.ElementType;

            switch (elementType.UnderlyingType.Category)
            {
            case Cts.TypeFlags.Boolean:
                return(new ConstantBooleanArray {
                    Value = GetCustomAttributeConstantArrayElements <bool>(value)
                });

            case Cts.TypeFlags.Byte:
                return(new ConstantByteArray {
                    Value = GetCustomAttributeConstantArrayElements <byte>(value)
                });

            case Cts.TypeFlags.Char:
                return(new ConstantCharArray {
                    Value = GetCustomAttributeConstantArrayElements <char>(value)
                });

            case Cts.TypeFlags.Double:
                return(new ConstantDoubleArray {
                    Value = GetCustomAttributeConstantArrayElements <double>(value)
                });

            case Cts.TypeFlags.Int16:
                return(new ConstantInt16Array {
                    Value = GetCustomAttributeConstantArrayElements <short>(value)
                });

            case Cts.TypeFlags.Int32:
                return(new ConstantInt32Array {
                    Value = GetCustomAttributeConstantArrayElements <int>(value)
                });

            case Cts.TypeFlags.Int64:
                return(new ConstantInt64Array {
                    Value = GetCustomAttributeConstantArrayElements <long>(value)
                });

            case Cts.TypeFlags.SByte:
                return(new ConstantSByteArray {
                    Value = GetCustomAttributeConstantArrayElements <sbyte>(value)
                });

            case Cts.TypeFlags.Single:
                return(new ConstantSingleArray {
                    Value = GetCustomAttributeConstantArrayElements <float>(value)
                });

            case Cts.TypeFlags.UInt16:
                return(new ConstantUInt16Array {
                    Value = GetCustomAttributeConstantArrayElements <ushort>(value)
                });

            case Cts.TypeFlags.UInt32:
                return(new ConstantUInt32Array {
                    Value = GetCustomAttributeConstantArrayElements <uint>(value)
                });

            case Cts.TypeFlags.UInt64:
                return(new ConstantUInt64Array {
                    Value = GetCustomAttributeConstantArrayElements <ulong>(value)
                });
            }

            if (elementType.IsString)
            {
                var record = new ConstantStringArray();
                record.Value.Capacity = value.Length;
                foreach (var element in value)
                {
                    MetadataRecord elementRecord = element.Value == null ?
                                                   (MetadataRecord) new ConstantReferenceValue() : HandleString((string)element.Value);
                    record.Value.Add(elementRecord);
                }
                return(record);
            }

            var result = new ConstantHandleArray();

            result.Value.Capacity = value.Length;
            for (int i = 0; i < value.Length; i++)
            {
                MetadataRecord elementRecord = HandleCustomAttributeConstantValue(value[i].Type, value[i].Value);
                if (value[i].Type.IsEnum)
                {
                    elementRecord = new ConstantBoxedEnumValue
                    {
                        Value = elementRecord,
                        Type  = HandleType(value[i].Type)
                    };
                }
                result.Value.Add(elementRecord);
            }

            return(result);
        }