Example #1
0
            private static unsafe bool BoxAndEquals(ref IntPtr data, IntPtr typeToBoxIntoPointer, object obj)
            {
                fixed (IntPtr* pData = &data)
                {
                    RuntimeTypeHandle typeToBoxInto = *(RuntimeTypeHandle*)&typeToBoxIntoPointer;

                    object boxedObject = RuntimeAugments.Box(typeToBoxInto, (IntPtr)pData);
                    return boxedObject.Equals(obj);
                }
            }
Example #2
0
            private static unsafe int BoxAndGetHashCode(ref IntPtr data, IntPtr typeToBoxIntoPointer)
            {
                fixed (IntPtr* pData = &data)
                {
                    RuntimeTypeHandle typeToBoxInto = *(RuntimeTypeHandle*)&typeToBoxIntoPointer;

                    object boxedObject = RuntimeAugments.Box(typeToBoxInto, (IntPtr)pData);
                    return boxedObject.GetHashCode();
                }
            }
Example #3
0
            private static unsafe string BoxAndToString(ref IntPtr data, IntPtr typeToBoxIntoPointer)
            {
                fixed(IntPtr *pData = &data)
                {
                    RuntimeTypeHandle typeToBoxInto = *(RuntimeTypeHandle *)&typeToBoxIntoPointer;

                    object boxedObject = RuntimeAugments.Box(typeToBoxInto, (IntPtr)pData);

                    return(boxedObject.ToString());
                }
            }
        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();
                }
            }
        }
Example #5
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();
                }
            }
        }