Beispiel #1
0
        //
        // Main routine to resolve a typeDef/Ref/Spec.
        //
        internal static RuntimeType Resolve(this ReflectionDomain reflectionDomain, MetadataReader reader, Handle typeDefRefOrSpec, TypeContext typeContext)
        {
            Exception   exception   = null;
            RuntimeType runtimeType = reflectionDomain.TryResolve(reader, typeDefRefOrSpec, typeContext, ref exception);

            if (runtimeType == null)
            {
                throw exception;
            }
            return(runtimeType);
        }
Beispiel #2
0
        // 
        // This is a port of the desktop CLR's RuntimeType.FormatTypeName() routine. This routine is used by various Reflection ToString() methods
        // to display the name of a type. Do not use for any other purpose as it inherits some pretty quirky desktop behavior.
        //
        // The Project N version takes a raw metadata handle rather than a completed type so that it remains robust in the face of missing metadata.
        //
        public static String FormatTypeName(this Handle typeDefRefOrSpecHandle, MetadataReader reader, TypeContext typeContext, ReflectionDomain reflectionDomain)
        {
            try
            {
                // Though we wrap this in a try-catch as a failsafe, this code must still strive to avoid triggering MissingMetadata exceptions
                // (non-error exceptions are very annoying when debugging.)

                Exception exception = null;
                RuntimeType runtimeType = reflectionDomain.TryResolve(reader, typeDefRefOrSpecHandle, typeContext, ref exception);
                if (runtimeType == null)
                    return UnavailableType;

                // Because this runtimeType came from a successful TryResolve() call, it is safe to querying the TypeInfo's of the type and its component parts.
                // If we're wrong, we do have the safety net of a try-catch.
                return runtimeType.FormatTypeName();
            }
            catch (Exception)
            {
                return UnavailableType;
            }
        }
Beispiel #3
0
        //
        // This is a port of the desktop CLR's RuntimeType.FormatTypeName() routine. This routine is used by various Reflection ToString() methods
        // to display the name of a type. Do not use for any other purpose as it inherits some pretty quirky desktop behavior.
        //
        // The Project N version takes a raw metadata handle rather than a completed type so that it remains robust in the face of missing metadata.
        //
        public static String FormatTypeName(this Handle typeDefRefOrSpecHandle, MetadataReader reader, TypeContext typeContext, ReflectionDomain reflectionDomain)
        {
            try
            {
                // Though we wrap this in a try-catch as a failsafe, this code must still strive to avoid triggering MissingMetadata exceptions
                // (non-error exceptions are very annoying when debugging.)

                Exception   exception   = null;
                RuntimeType runtimeType = reflectionDomain.TryResolve(reader, typeDefRefOrSpecHandle, typeContext, ref exception);
                if (runtimeType == null)
                {
                    return(UnavailableType);
                }

                // Because this runtimeType came from a successful TryResolve() call, it is safe to querying the TypeInfo's of the type and its component parts.
                // If we're wrong, we do have the safety net of a try-catch.
                return(runtimeType.FormatTypeName());
            }
            catch (Exception)
            {
                return(UnavailableType);
            }
        }
Beispiel #4
0
        public static Exception TryParseConstantValue(this Handle handle, ReflectionDomain reflectionDomain, MetadataReader reader, out Object value)
        {
            HandleType handleType = handle.HandleType;

            switch (handleType)
            {
            case HandleType.ConstantBooleanValue:
                value = handle.ToConstantBooleanValueHandle(reader).GetConstantBooleanValue(reader).Value;
                return(null);

            case HandleType.ConstantStringValue:
                value = handle.ToConstantStringValueHandle(reader).GetConstantStringValue(reader).Value;
                return(null);

            case HandleType.ConstantCharValue:
                value = handle.ToConstantCharValueHandle(reader).GetConstantCharValue(reader).Value;
                return(null);

            case HandleType.ConstantByteValue:
                value = handle.ToConstantByteValueHandle(reader).GetConstantByteValue(reader).Value;
                return(null);

            case HandleType.ConstantSByteValue:
                value = handle.ToConstantSByteValueHandle(reader).GetConstantSByteValue(reader).Value;
                return(null);

            case HandleType.ConstantInt16Value:
                value = handle.ToConstantInt16ValueHandle(reader).GetConstantInt16Value(reader).Value;
                return(null);

            case HandleType.ConstantUInt16Value:
                value = handle.ToConstantUInt16ValueHandle(reader).GetConstantUInt16Value(reader).Value;
                return(null);

            case HandleType.ConstantInt32Value:
                value = handle.ToConstantInt32ValueHandle(reader).GetConstantInt32Value(reader).Value;
                return(null);

            case HandleType.ConstantUInt32Value:
                value = handle.ToConstantUInt32ValueHandle(reader).GetConstantUInt32Value(reader).Value;
                return(null);

            case HandleType.ConstantInt64Value:
                value = handle.ToConstantInt64ValueHandle(reader).GetConstantInt64Value(reader).Value;
                return(null);

            case HandleType.ConstantUInt64Value:
                value = handle.ToConstantUInt64ValueHandle(reader).GetConstantUInt64Value(reader).Value;
                return(null);

            case HandleType.ConstantSingleValue:
                value = handle.ToConstantSingleValueHandle(reader).GetConstantSingleValue(reader).Value;
                return(null);

            case HandleType.ConstantDoubleValue:
                value = handle.ToConstantDoubleValueHandle(reader).GetConstantDoubleValue(reader).Value;
                return(null);

            case HandleType.TypeDefinition:
            case HandleType.TypeReference:
            case HandleType.TypeSpecification:
            {
                Exception exception = null;
                value = reflectionDomain.TryResolve(reader, handle, new TypeContext(null, null), ref exception);
                return((value == null) ? exception : null);
            }

            case HandleType.ConstantReferenceValue:
                value = null;
                return(null);

            case HandleType.ConstantBoxedEnumValue:
            {
                return(handle.ToConstantBoxedEnumValueHandle(reader).ParseBoxedEnumConstantValue(reflectionDomain, reader, out value));
            }

            default:
            {
                Exception exception;
                value = handle.TryParseConstantArray(reflectionDomain, reader, out exception);
                if (value == null)
                {
                    return(exception);
                }
                return(null);
            }
            }
        }
Beispiel #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();
                }
            }
        }
Beispiel #6
0
        //
        // Main routine to parse a metadata type specification signature.
        //
        private static RuntimeType TryResolveTypeSignature(this ReflectionDomain reflectionDomain, MetadataReader reader, TypeSpecificationHandle typeSpecHandle, TypeContext typeContext, ref Exception exception)
        {
            Handle typeHandle = typeSpecHandle.GetTypeSpecification(reader).Signature;

            switch (typeHandle.HandleType)
            {
            case HandleType.ArraySignature:
            {
                ArraySignature sig  = typeHandle.ToArraySignatureHandle(reader).GetArraySignature(reader);
                int            rank = sig.Rank;
                if (rank <= 0)
                {
                    throw new BadImageFormatException();         // Bad rank.
                }
                RuntimeType elementType = reflectionDomain.TryResolve(reader, sig.ElementType, typeContext, ref exception);
                if (elementType == null)
                {
                    return(null);
                }
                return(ReflectionCoreNonPortable.GetMultiDimArrayType(elementType, rank));
            }

            case HandleType.ByReferenceSignature:
            {
                ByReferenceSignature sig        = typeHandle.ToByReferenceSignatureHandle(reader).GetByReferenceSignature(reader);
                RuntimeType          targetType = reflectionDomain.TryResolve(reader, sig.Type, typeContext, ref exception);
                if (targetType == null)
                {
                    return(null);
                }
                return(ReflectionCoreNonPortable.GetByRefType(targetType));
            }

            case HandleType.MethodTypeVariableSignature:
            {
                MethodTypeVariableSignature sig = typeHandle.ToMethodTypeVariableSignatureHandle(reader).GetMethodTypeVariableSignature(reader);
                return(typeContext.GenericMethodArguments[sig.Number]);
            }

            case HandleType.PointerSignature:
            {
                PointerSignature sig        = typeHandle.ToPointerSignatureHandle(reader).GetPointerSignature(reader);
                RuntimeType      targetType = reflectionDomain.TryResolve(reader, sig.Type, typeContext, ref exception);
                if (targetType == null)
                {
                    return(null);
                }
                return(ReflectionCoreNonPortable.GetPointerType(targetType));
            }

            case HandleType.SZArraySignature:
            {
                SZArraySignature sig         = typeHandle.ToSZArraySignatureHandle(reader).GetSZArraySignature(reader);
                RuntimeType      elementType = reflectionDomain.TryResolve(reader, sig.ElementType, typeContext, ref exception);
                if (elementType == null)
                {
                    return(null);
                }
                return(ReflectionCoreNonPortable.GetArrayType(elementType));
            }

            case HandleType.TypeDefinition:
            {
                return(reflectionDomain.ResolveTypeDefinition(reader, typeHandle.ToTypeDefinitionHandle(reader)));
            }

            case HandleType.TypeInstantiationSignature:
            {
                TypeInstantiationSignature sig    = typeHandle.ToTypeInstantiationSignatureHandle(reader).GetTypeInstantiationSignature(reader);
                RuntimeType genericTypeDefinition = reflectionDomain.TryResolve(reader, sig.GenericType, typeContext, ref exception);
                if (genericTypeDefinition == null)
                {
                    return(null);
                }
                LowLevelList <RuntimeType> genericTypeArguments = new LowLevelList <RuntimeType>();
                foreach (Handle genericTypeArgumentHandle in sig.GenericTypeArguments)
                {
                    RuntimeType genericTypeArgument = reflectionDomain.TryResolve(reader, genericTypeArgumentHandle, typeContext, ref exception);
                    if (genericTypeArgument == null)
                    {
                        return(null);
                    }
                    genericTypeArguments.Add(genericTypeArgument);
                }
                return(ReflectionCoreNonPortable.GetConstructedGenericType(genericTypeDefinition, genericTypeArguments.ToArray()));
            }

            case HandleType.TypeReference:
            {
                return(reflectionDomain.TryResolveTypeReference(reader, typeHandle.ToTypeReferenceHandle(reader), ref exception));
            }

            case HandleType.TypeVariableSignature:
            {
                TypeVariableSignature sig = typeHandle.ToTypeVariableSignatureHandle(reader).GetTypeVariableSignature(reader);
                return(typeContext.GenericTypeArguments[sig.Number]);
            }

            default:
                throw new NotSupportedException();     // Unexpected Type signature type.
            }
        }
 public static Exception TryParseConstantValue(this Handle handle, ReflectionDomain reflectionDomain, MetadataReader reader, out Object value)
 {
     HandleType handleType = handle.HandleType;
     switch (handleType)
     {
         case HandleType.ConstantBooleanValue:
             value = handle.ToConstantBooleanValueHandle(reader).GetConstantBooleanValue(reader).Value;
             return null;
         case HandleType.ConstantStringValue:
             value = handle.ToConstantStringValueHandle(reader).GetConstantStringValue(reader).Value;
             return null;
         case HandleType.ConstantCharValue:
             value = handle.ToConstantCharValueHandle(reader).GetConstantCharValue(reader).Value;
             return null;
         case HandleType.ConstantByteValue:
             value = handle.ToConstantByteValueHandle(reader).GetConstantByteValue(reader).Value;
             return null;
         case HandleType.ConstantSByteValue:
             value = handle.ToConstantSByteValueHandle(reader).GetConstantSByteValue(reader).Value;
             return null;
         case HandleType.ConstantInt16Value:
             value = handle.ToConstantInt16ValueHandle(reader).GetConstantInt16Value(reader).Value;
             return null;
         case HandleType.ConstantUInt16Value:
             value = handle.ToConstantUInt16ValueHandle(reader).GetConstantUInt16Value(reader).Value;
             return null;
         case HandleType.ConstantInt32Value:
             value = handle.ToConstantInt32ValueHandle(reader).GetConstantInt32Value(reader).Value;
             return null;
         case HandleType.ConstantUInt32Value:
             value = handle.ToConstantUInt32ValueHandle(reader).GetConstantUInt32Value(reader).Value;
             return null;
         case HandleType.ConstantInt64Value:
             value = handle.ToConstantInt64ValueHandle(reader).GetConstantInt64Value(reader).Value;
             return null;
         case HandleType.ConstantUInt64Value:
             value = handle.ToConstantUInt64ValueHandle(reader).GetConstantUInt64Value(reader).Value;
             return null;
         case HandleType.ConstantSingleValue:
             value = handle.ToConstantSingleValueHandle(reader).GetConstantSingleValue(reader).Value;
             return null;
         case HandleType.ConstantDoubleValue:
             value = handle.ToConstantDoubleValueHandle(reader).GetConstantDoubleValue(reader).Value;
             return null;
         case HandleType.TypeDefinition:
         case HandleType.TypeReference:
         case HandleType.TypeSpecification:
             {
                 Exception exception = null;
                 value = reflectionDomain.TryResolve(reader, handle, new TypeContext(null, null), ref exception);
                 return (value == null) ? exception : null;
             }
         case HandleType.ConstantReferenceValue:
             value = null;
             return null;
         case HandleType.ConstantBoxedEnumValue:
             {
                 return handle.ToConstantBoxedEnumValueHandle(reader).ParseBoxedEnumConstantValue(reflectionDomain, reader, out value);
             }
         default:
             {
                 Exception exception;
                 value = handle.TryParseConstantArray(reflectionDomain, reader, out exception);
                 if (value == null)
                     return exception;
                 return null;
             }
     }
 }
        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();
                }
            }

        }