internal static MissingMetadataException Create(TypeInfo pertainant, string nestedTypeName)
        {
            if (pertainant == null)
            {
                return(new MissingMetadataException(SR.Format(SR.Reflection_InsufficientMetadata_NoHelpAvailable, "<unavailable>")));
            }

            String usefulPertainant = ComputeUsefulPertainantIfPossible(pertainant);

            if (usefulPertainant == null)
            {
                return(new MissingMetadataException(Format(SR.Reflection_InsufficientMetadata_NoHelpAvailable, pertainant.ToString())));
            }
            else
            {
                usefulPertainant = usefulPertainant + "." + DiagnosticMappingTables.ConvertBackTickNameToNameWithReducerInputFormat(nestedTypeName, null);
                return(new MissingMetadataException(Format(SR.Reflection_InsufficientMetadata_EdbNeeded, usefulPertainant)));
            }
        }
Esempio n. 2
0
        public static bool TryGetMultiDimArrayTypeElementType(RuntimeTypeHandle arrayTypeHandle, int rank, out RuntimeTypeHandle elementTypeHandle)
        {
            elementTypeHandle = default(RuntimeTypeHandle);
            if (rank != 2)
            {
                return(false);
            }

            // Rank 2 arrays are really generic type of type MDArrayRank2<T>.
            RuntimeTypeHandle genericTypeDefinitionHandle;

            RuntimeTypeHandle[] genericTypeArgumentHandles;
            if (!DiagnosticMappingTables.TryGetConstructedGenericTypeComponents(arrayTypeHandle, out genericTypeDefinitionHandle, out genericTypeArgumentHandles))
            {
                return(false);
            }
            // This should really be an assert but this is used for generating diagnostic info so it's better to persevere and hope something useful gets printed out.
            if (genericTypeArgumentHandles.Length != 1)
            {
                return(false);
            }
            elementTypeHandle = genericTypeArgumentHandles[0];
            return(true);
        }
        internal static String ToDisplayStringIfAvailable(this Type type, List <int> genericParameterOffsets)
        {
            RuntimeTypeHandle runtimeTypeHandle = ReflectionCoreExecution.ExecutionDomain.GetTypeHandleIfAvailable(type);
            bool hasRuntimeTypeHandle           = !runtimeTypeHandle.Equals(default(RuntimeTypeHandle));

            if (type.HasElementType)
            {
                if (type.IsArray)
                {
                    // Multidim arrays. This is the one case where GetElementType() isn't pay-for-play safe so
                    // talk to the diagnostic mapping tables directly if possible or give up.
                    if (!hasRuntimeTypeHandle)
                    {
                        return(null);
                    }

                    int rank = type.GetArrayRank();
                    return(CreateArrayTypeStringIfAvailable(type.GetElementType(), rank));
                }
                else
                {
                    String s = type.GetElementType().ToDisplayStringIfAvailable(null);
                    if (s == null)
                    {
                        return(null);
                    }
                    return(s + (type.IsPointer ? "*" : "&"));
                }
            }
            else if (((hasRuntimeTypeHandle && RuntimeAugments.IsGenericType(runtimeTypeHandle)) || type.IsConstructedGenericType))
            {
                Type   genericTypeDefinition;
                Type[] genericTypeArguments;
                if (hasRuntimeTypeHandle)
                {
                    RuntimeTypeHandle   genericTypeDefinitionHandle;
                    RuntimeTypeHandle[] genericTypeArgumentHandles;

                    genericTypeDefinitionHandle = RuntimeAugments.GetGenericInstantiation(runtimeTypeHandle, out genericTypeArgumentHandles);
                    genericTypeDefinition       = Type.GetTypeFromHandle(genericTypeDefinitionHandle);
                    genericTypeArguments        = new Type[genericTypeArgumentHandles.Length];
                    for (int i = 0; i < genericTypeArguments.Length; i++)
                    {
                        genericTypeArguments[i] = Type.GetTypeFromHandle(genericTypeArgumentHandles[i]);
                    }
                }
                else
                {
                    genericTypeDefinition = type.GetGenericTypeDefinition();
                    genericTypeArguments  = type.GenericTypeArguments;
                }

                return(CreateConstructedGenericTypeStringIfAvailable(genericTypeDefinition, genericTypeArguments));
            }
            else if (type.IsGenericParameter)
            {
                return(type.Name);
            }
            else if (hasRuntimeTypeHandle)
            {
                String s;
                if (!DiagnosticMappingTables.TryGetDiagnosticStringForNamedType(runtimeTypeHandle, out s, genericParameterOffsets))
                {
                    return(null);
                }

                return(s);
            }
            else
            {
                return(null);
            }
        }
        internal static string ToDisplayStringIfAvailable(this Type type, List <int> genericParameterOffsets)
        {
            RuntimeTypeHandle runtimeTypeHandle = ReflectionCoreExecution.ExecutionDomain.GetTypeHandleIfAvailable(type);
            bool hasRuntimeTypeHandle           = !runtimeTypeHandle.Equals(default(RuntimeTypeHandle));

            if (type.HasElementType)
            {
                if (type.IsArray)
                {
                    // Multidim arrays. This is the one case where GetElementType() isn't pay-for-play safe so
                    // talk to the diagnostic mapping tables directly if possible or give up.
                    if (!hasRuntimeTypeHandle)
                    {
                        return(null);
                    }

                    int rank = type.GetArrayRank();
                    return(CreateArrayTypeStringIfAvailable(type.GetElementType(), rank));
                }
                else
                {
                    string s = type.GetElementType().ToDisplayStringIfAvailable(null);
                    if (s == null)
                    {
                        return(null);
                    }
                    return(s + (type.IsPointer ? "*" : "&"));
                }
            }
            else if (((hasRuntimeTypeHandle && RuntimeAugments.IsGenericType(runtimeTypeHandle)) || type.IsConstructedGenericType))
            {
                Type   genericTypeDefinition;
                Type[] genericTypeArguments;
                if (hasRuntimeTypeHandle)
                {
                    RuntimeTypeHandle   genericTypeDefinitionHandle;
                    RuntimeTypeHandle[] genericTypeArgumentHandles;

                    genericTypeDefinitionHandle = RuntimeAugments.GetGenericInstantiation(runtimeTypeHandle, out genericTypeArgumentHandles);
                    genericTypeDefinition       = Type.GetTypeFromHandle(genericTypeDefinitionHandle);
                    genericTypeArguments        = new Type[genericTypeArgumentHandles.Length];
                    for (int i = 0; i < genericTypeArguments.Length; i++)
                    {
                        genericTypeArguments[i] = Type.GetTypeFromHandle(genericTypeArgumentHandles[i]);
                    }
                }
                else
                {
                    genericTypeDefinition = type.GetGenericTypeDefinition();
                    genericTypeArguments  = type.GenericTypeArguments;
                }

                return(CreateConstructedGenericTypeStringIfAvailable(genericTypeDefinition, genericTypeArguments));
            }
            else if (type.IsGenericParameter)
            {
                return(type.Name);
            }
            else if (hasRuntimeTypeHandle)
            {
                string s;
                if (!DiagnosticMappingTables.TryGetDiagnosticStringForNamedType(runtimeTypeHandle, out s, genericParameterOffsets))
                {
                    return(null);
                }

                return(s);
            }
            else
            {
                // First, see if Type.Name is available. If Type.Name is available, then we can be reasonably confident that it is safe to call Type.FullName.
                // We'll still wrap the call in a try-catch as a failsafe.
                string s = type.InternalNameIfAvailable;
                if (s == null)
                {
                    return(null);
                }

                try
                {
                    s = type.FullName;
                }
                catch (MissingMetadataException)
                {
                }

                // Insert commas so that CreateConstructedGenericTypeStringIfAvailable can fill the blanks.
                // This is not strictly correct for types nested under generic types, but at this point we're doing
                // best effort within reason.
                if (type.IsGenericTypeDefinition)
                {
                    s += "[";
                    int genericArgCount = type.GetGenericArguments().Length;
                    while (genericArgCount-- > 0)
                    {
                        genericParameterOffsets.Add(s.Length);
                        if (genericArgCount > 0)
                        {
                            s = s + ",";
                        }
                    }
                    s += "]";
                }

                return(s);
            }
        }
        internal static String ToDisplayStringIfAvailable(this Type type, List <int> genericParameterOffsets)
        {
            RuntimeTypeHandle runtimeTypeHandle = RuntimeAugments.GetTypeHandleIfAvailable(type);
            bool hasRuntimeTypeHandle           = !runtimeTypeHandle.Equals(default(RuntimeTypeHandle));

            if (type.HasElementType)
            {
                if (type.IsArray)
                {
                    int rank = type.GetArrayRank();
                    if (rank == 1)
                    {
                        // Multidims of rank 1 are not supported on Project N so we can safely assume this is not the multidim case.
                        // Unfortunately, I can't assert it here because Reflection doesn't have any api to distinguish these cases.
                        return(CreateArrayTypeStringIfAvailable(type.GetElementType(), false, 1));
                    }
                    else
                    {
                        // Multidim arrays. This is the one case where GetElementType() isn't pay-for-play safe so
                        // talk to the diagnostic mapping tables directly if possible or give up.
                        if (!hasRuntimeTypeHandle)
                        {
                            return(null);
                        }

                        RuntimeTypeHandle elementTypeHandle;
                        if (!DiagnosticMappingTables.TryGetMultiDimArrayTypeElementType(runtimeTypeHandle, rank, out elementTypeHandle))
                        {
                            return(null);
                        }
                        Type elementType = Type.GetTypeFromHandle(elementTypeHandle);
                        return(CreateArrayTypeStringIfAvailable(elementType, true, rank));
                    }
                }
                else
                {
                    String s = type.GetElementType().ToDisplayStringIfAvailable(null);
                    if (s == null)
                    {
                        return(null);
                    }
                    return(s + (type.IsPointer ? "*" : "&"));
                }
            }
            else if (((hasRuntimeTypeHandle && RuntimeAugments.IsGenericType(runtimeTypeHandle)) || type.IsConstructedGenericType))
            {
                Type   genericTypeDefinition;
                Type[] genericTypeArguments;
                if (hasRuntimeTypeHandle)
                {
                    RuntimeTypeHandle   genericTypeDefinitionHandle;
                    RuntimeTypeHandle[] genericTypeArgumentHandles;

                    if (!DiagnosticMappingTables.TryGetConstructedGenericTypeComponents(runtimeTypeHandle, out genericTypeDefinitionHandle, out genericTypeArgumentHandles))
                    {
                        return(null);
                    }
                    genericTypeDefinition = Type.GetTypeFromHandle(genericTypeDefinitionHandle);
                    genericTypeArguments  = new Type[genericTypeArgumentHandles.Length];
                    for (int i = 0; i < genericTypeArguments.Length; i++)
                    {
                        genericTypeArguments[i] = Type.GetTypeFromHandle(genericTypeArgumentHandles[i]);
                    }
                }
                else
                {
                    genericTypeDefinition = type.GetGenericTypeDefinition();
                    genericTypeArguments  = type.GenericTypeArguments;
                }

                return(CreateConstructedGenericTypeStringIfAvailable(genericTypeDefinition, genericTypeArguments));
            }
            else if (type.IsGenericParameter)
            {
                return(type.Name);
            }
            else if (hasRuntimeTypeHandle)
            {
                String s;
                if (!DiagnosticMappingTables.TryGetDiagnosticStringForNamedType(runtimeTypeHandle, out s, genericParameterOffsets))
                {
                    return(null);
                }

                return(s);
            }
            else
            {
                return(null);
            }
        }