protected RuntimeMethodParameterInfo(MethodBase member, int position, MetadataReader reader, Handle typeHandle, TypeContext typeContext)
     : base(member, position)
 {
     Reader = reader;
     _typeHandle = typeHandle;
     _typeContext = typeContext;
 }
 private RuntimeFatMethodParameterInfo(MethodBase member, MethodHandle methodHandle, int position, ParameterHandle parameterHandle, MetadataReader reader, Handle typeHandle, TypeContext typeContext)
     : base(member, position, reader, typeHandle, typeContext)
 {
     _methodHandle = methodHandle;
     _parameterHandle = parameterHandle;
     _parameter = parameterHandle.GetParameter(reader);
 }
 private NativeFormatMethodParameterInfo(MethodBase member, MethodHandle methodHandle, int position, ParameterHandle parameterHandle, QTypeDefRefOrSpec qualifiedParameterTypeHandle, TypeContext typeContext)
     : base(member, position, qualifiedParameterTypeHandle, typeContext)
 {
     _methodHandle = methodHandle;
     _parameterHandle = parameterHandle;
     _parameter = parameterHandle.GetParameter(Reader);
 }
Example #4
0
 //
 // Main routine to resolve a typeDef/Ref/Spec.
 //
 internal static RuntimeTypeInfo Resolve(this QTypeDefRefOrSpec typeDefOrRefOrSpec, TypeContext typeContext)
 {
     Exception exception = null;
     RuntimeTypeInfo runtimeType = typeDefOrRefOrSpec.TryResolve(typeContext, ref exception);
     if (runtimeType == null)
         throw exception;
     return runtimeType;
 }
 //
 // Main routine to resolve a typeDef/Ref/Spec.
 //
 internal static RuntimeTypeInfo Resolve(this Handle typeDefRefOrSpec, MetadataReader reader, TypeContext typeContext)
 {
     Exception exception = null;
     RuntimeTypeInfo runtimeType = typeDefRefOrSpec.TryResolve(reader, typeContext, ref exception);
     if (runtimeType == null)
         throw exception;
     return runtimeType;
 }
Example #6
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;
 }
Example #7
0
        internal static RuntimeTypeInfo TryResolve(this QTypeDefRefOrSpec typeDefOrRefOrSpec, TypeContext typeContext, ref Exception exception)
        {
            if (typeDefOrRefOrSpec.IsNativeFormatMetadataBased)
            {
                return global::Internal.Metadata.NativeFormat.Handle.FromIntToken(typeDefOrRefOrSpec.Handle).TryResolve((global::Internal.Metadata.NativeFormat.MetadataReader)typeDefOrRefOrSpec.Reader, typeContext, ref exception);
            }

            throw new BadImageFormatException();  // Expected TypeRef, Def or Spec with MetadataReader
        }
Example #8
0
 internal static RuntimeType TryResolve(this ReflectionDomain reflectionDomain, MetadataReader reader, Handle typeDefRefOrSpec, TypeContext typeContext, ref Exception exception)
 {
     HandleType handleType = typeDefRefOrSpec.HandleType;
     if (handleType == HandleType.TypeDefinition)
         return reflectionDomain.ResolveTypeDefinition(reader, typeDefRefOrSpec.ToTypeDefinitionHandle(reader));
     else if (handleType == HandleType.TypeReference)
         return reflectionDomain.TryResolveTypeReference(reader, typeDefRefOrSpec.ToTypeReferenceHandle(reader), ref exception);
     else if (handleType == HandleType.TypeSpecification)
         return reflectionDomain.TryResolveTypeSignature(reader, typeDefRefOrSpec.ToTypeSpecificationHandle(reader), typeContext, ref exception);
     else
         throw new BadImageFormatException();  // Expected TypeRef, Def or Spec.
 }
Example #9
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 QTypeDefRefOrSpec qualifiedTypeHandle, TypeContext typeContext)
        {
            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;
                RuntimeTypeInfo runtimeType = qualifiedTypeHandle.TryResolve(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;
            }
        }
Example #10
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.
            }
        }
Example #11
0
        //
        // Main routine to resolve a typeDef/Ref/Spec. Also accepts ModifiedTypes (will unwrap and ignore the custom modifiers.)
        //
        internal static RuntimeTypeInfo Resolve(this Handle typeDefRefOrSpec, MetadataReader reader, TypeContext typeContext)
        {
            Exception?      exception   = null;
            RuntimeTypeInfo?runtimeType = typeDefRefOrSpec.TryResolve(reader, typeContext, ref exception);

            if (runtimeType == null)
            {
                throw exception !;
            }
            return(runtimeType);
        }
Example #12
0
        internal static RuntimeType TryResolve(this ReflectionDomain reflectionDomain, MetadataReader reader, Handle typeDefRefOrSpec, TypeContext typeContext, ref Exception exception)
        {
            HandleType handleType = typeDefRefOrSpec.HandleType;

            if (handleType == HandleType.TypeDefinition)
            {
                return(reflectionDomain.ResolveTypeDefinition(reader, typeDefRefOrSpec.ToTypeDefinitionHandle(reader)));
            }
            else if (handleType == HandleType.TypeReference)
            {
                return(reflectionDomain.TryResolveTypeReference(reader, typeDefRefOrSpec.ToTypeReferenceHandle(reader), ref exception));
            }
            else if (handleType == HandleType.TypeSpecification)
            {
                return(reflectionDomain.TryResolveTypeSignature(reader, typeDefRefOrSpec.ToTypeSpecificationHandle(reader), typeContext, ref exception));
            }
            else
            {
                throw new BadImageFormatException();  // Expected TypeRef, Def or Spec.
            }
        }
Example #13
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);
        }
Example #14
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.
            }
        }
 private RuntimeThinMethodParameterInfo(MethodBase member, int position, QTypeDefRefOrSpec qualifiedParameterTypeHandle, TypeContext typeContext)
     : base(member, position, qualifiedParameterTypeHandle, typeContext)
 {
 }
        // Return any custom modifiers modifying the passed-in type and whose required/optional bit matches the passed in boolean.
        // Because this is intended to service the GetCustomModifiers() apis, this helper will always return a freshly allocated array
        // safe for returning to api callers.
        internal static Type[] GetCustomModifiers(this Handle handle, MetadataReader reader, TypeContext typeContext, bool optional)
        {
            HandleType handleType = handle.HandleType;

            Debug.Assert(handleType == HandleType.TypeDefinition || handleType == HandleType.TypeReference || handleType == HandleType.TypeSpecification || handleType == HandleType.ModifiedType);
            if (handleType != HandleType.ModifiedType)
            {
                return(Array.Empty <Type>());
            }

            LowLevelList <Type> customModifiers = new LowLevelList <Type>();

            do
            {
                ModifiedType modifiedType = handle.ToModifiedTypeHandle(reader).GetModifiedType(reader);
                if (optional == modifiedType.IsOptional)
                {
                    Type customModifier = modifiedType.ModifierType.Resolve(reader, typeContext);
                    customModifiers.Insert(0, customModifier);
                }

                handle     = modifiedType.Type;
                handleType = handle.HandleType;
            }while (handleType == HandleType.ModifiedType);
            return(customModifiers.ToArray());
        }
 protected RuntimeMethodParameterInfo(MethodBase member, int position, QTypeDefRefOrSpec qualifiedParameterTypeHandle, TypeContext typeContext)
     : base(member, position)
 {
     QualifiedParameterTypeHandle = qualifiedParameterTypeHandle;
     _typeContext = typeContext;
 }
Example #18
0
        //
        // Returns the ParameterInfo objects for the return parameter (in element 0), and the method parameters (in elements 1..length).
        //
        // The ParameterInfo objects will report "contextMethod" as their Member property and use it to get type variable information from
        // the contextMethod's declaring type. The actual metadata, however, comes from "this."
        //
        // The methodTypeArguments provides the fill-ins for any method type variable elements in the parameter type signatures.
        //
        // Does not array-copy.
        //
        public RuntimeMethodParameterInfo[] GetRuntimeParametersAndReturn(MethodBase contextMethod, RuntimeTypeInfo[] methodTypeArguments)
        {
            MetadataReader reader = _reader;
            TypeContext typeContext = contextMethod.DeclaringType.CastToRuntimeTypeInfo().TypeContext;
            typeContext = new TypeContext(typeContext.GenericTypeArguments, methodTypeArguments);
            MethodSignature methodSignature = this.MethodSignature;
            Handle[] typeSignatures = new Handle[methodSignature.Parameters.Count + 1];
            typeSignatures[0] = methodSignature.ReturnType;
            int paramIndex = 1;
            foreach (Handle parameterTypeSignatureHandle in methodSignature.Parameters)
            {
                typeSignatures[paramIndex++] = parameterTypeSignatureHandle;
            }
            int count = typeSignatures.Length;

            RuntimeMethodParameterInfo[] result = new RuntimeMethodParameterInfo[count];
            foreach (ParameterHandle parameterHandle in _method.Parameters)
            {
                Parameter parameterRecord = parameterHandle.GetParameter(_reader);
                int index = parameterRecord.Sequence;
                result[index] =
                    RuntimeFatMethodParameterInfo.GetRuntimeFatMethodParameterInfo(
                        contextMethod,
                        _methodHandle,
                        index - 1,
                        parameterHandle,
                        reader,
                        typeSignatures[index],
                        typeContext);
            }
            for (int i = 0; i < count; i++)
            {
                if (result[i] == null)
                {
                    result[i] =
                        RuntimeThinMethodParameterInfo.GetRuntimeThinMethodParameterInfo(
                            contextMethod,
                            i - 1,
                        reader,
                        typeSignatures[i],
                        typeContext);
                }
            }
            return result;
        }
        // Return any custom modifiers modifying the passed-in type and whose required/optional bit matches the passed in boolean.
        // Because this is intended to service the GetCustomModifiers() apis, this helper will always return a freshly allocated array
        // safe for returning to api callers.
        internal                                                       Type[] GetCustomModifiers(TypeContext typeContext, bool optional)
        {
#if ECMA_METADATA_SUPPORT
            throw new NotImplementedException();
#else
            return(_handle.GetCustomModifiers((global::Internal.Metadata.NativeFormat.MetadataReader)Reader, typeContext, optional));
#endif
        }
 private RuntimeThinMethodParameterInfo(MethodBase member, int position, MetadataReader reader, Handle typeHandle, TypeContext typeContext)
     : base(member, position, reader, typeHandle, typeContext)
 {
 }
Example #21
0
        internal static RuntimeTypeInfo?TryResolve(this Handle typeDefRefOrSpec, MetadataReader reader, TypeContext typeContext, ref Exception?exception)
        {
            HandleType handleType = typeDefRefOrSpec.HandleType;

            if (handleType == HandleType.TypeDefinition)
            {
                return(typeDefRefOrSpec.ToTypeDefinitionHandle(reader).ResolveTypeDefinition(reader));
            }
            else if (handleType == HandleType.TypeReference)
            {
                return(typeDefRefOrSpec.ToTypeReferenceHandle(reader).TryResolveTypeReference(reader, ref exception));
            }
            else if (handleType == HandleType.TypeSpecification)
            {
                return(typeDefRefOrSpec.ToTypeSpecificationHandle(reader).TryResolveTypeSignature(reader, typeContext, ref exception));
            }
            else if (handleType == HandleType.ModifiedType)
            {
                ModifiedType modifiedType = typeDefRefOrSpec.ToModifiedTypeHandle(reader).GetModifiedType(reader);
                return(modifiedType.Type.TryResolve(reader, typeContext, ref exception));
            }
            else
            {
                throw new BadImageFormatException();  // Expected TypeRef, Def or Spec.
            }
        }
Example #22
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);
            }
        }
        //
        // Main routine to resolve a typeDef/Ref/Spec.
        //
        internal static RuntimeTypeInfo Resolve(this QTypeDefRefOrSpec typeDefOrRefOrSpec, TypeContext typeContext)
        {
            Exception       exception   = null;
            RuntimeTypeInfo runtimeType = typeDefOrRefOrSpec.TryResolve(typeContext, ref exception);

            if (runtimeType == null)
            {
                throw exception;
            }
            return(runtimeType);
        }
        internal static RuntimeTypeInfo TryResolve(this QTypeDefRefOrSpec typeDefOrRefOrSpec, TypeContext typeContext, ref Exception exception)
        {
            if (typeDefOrRefOrSpec.IsNativeFormatMetadataBased)
            {
                return(global::Internal.Metadata.NativeFormat.Handle.FromIntToken(typeDefOrRefOrSpec.Handle).TryResolve((global::Internal.Metadata.NativeFormat.MetadataReader)typeDefOrRefOrSpec.Reader, typeContext, ref exception));
            }

#if ECMA_METADATA_SUPPORT
            if (typeDefOrRefOrSpec.Reader is global::System.Reflection.Metadata.MetadataReader ecmaReader)
            {
                return(global::System.Reflection.Metadata.Ecma335.MetadataTokens.Handle(typeDefOrRefOrSpec.Handle).TryResolve(ecmaReader, typeContext, ref exception));
            }
#endif

            throw new BadImageFormatException();  // Expected TypeRef, Def or Spec with MetadataReader
        }