Esempio n. 1
0
        /// <summary>
        /// Returns the name of the type as specified by the TypeHandle.  Note this returns the name as specified by the
        /// metadata, NOT as you would expect to see it in a C# program.  For example, generics are denoted with a ` and
        /// the number of params.  Thus a Dictionary (with two type params) would look like:
        /// System.Collections.Generics.Dictionary`2
        /// </summary>
        /// <param name="id">The TypeHandle to get the name of.</param>
        /// <returns>The name of the type, or null on error.</returns>
        internal string GetTypeName(TypeHandle id)
        {
            if (id.MethodTable == FreeMethodTable)
            {
                return("Free");
            }

            if (id.MethodTable == ArrayMethodTable && id.ComponentMethodTable != 0)
            {
                string name = GetNameForMT(id.ComponentMethodTable);
                if (name != null)
                {
                    return(name + "[]");
                }
            }

            return(GetNameForMT(id.MethodTable));
        }
Esempio n. 2
0
        internal override ClrType GetTypeByMethodTable(ulong mt, ulong cmt, ulong obj)
        {
            if (mt == 0)
            {
                return(null);
            }

            ClrType componentType = null;

            if (mt == DesktopRuntime.ArrayMethodTable)
            {
                if (cmt != 0)
                {
                    componentType = GetTypeByMethodTable(cmt, 0);
                    if (componentType != null)
                    {
                        cmt = componentType.MethodTable;
                    }
                    else if (obj != 0)
                    {
                        componentType = TryGetComponentType(obj, cmt);
                        if (componentType != null)
                        {
                            cmt = componentType.MethodTable;
                        }
                    }
                }
                else
                {
                    componentType = ObjectType;
                    cmt           = ObjectType.MethodTable;
                }
            }
            else
            {
                cmt = 0;
            }

            TypeHandle hnd = new TypeHandle(mt, cmt);
            ClrType    ret = null;

            // See if we already have the type.
            if (_indices.TryGetValue(hnd, out int index))
            {
                ret = _types[index];
            }
            else if (mt == DesktopRuntime.ArrayMethodTable && cmt == 0)
            {
                // Handle the case where the methodtable is an array, but the component method table
                // was not specified.  (This happens with fields.)  In this case, return System.Object[],
                // with an ArrayComponentType set to System.Object.
                uint token = DesktopRuntime.GetMetadataToken(mt);
                if (token == 0xffffffff)
                {
                    return(null);
                }

                ModuleEntry modEnt = new ModuleEntry(ArrayType.Module, token);

                ret   = ArrayType;
                index = _types.Count;

                _indices[hnd]      = index;
                _typeEntry[modEnt] = index;
                _types.Add(ret);

                Debug.Assert(_types[index] == ret);
            }
            else
            {
                // No, so we'll have to construct it.
                ulong         moduleAddr = DesktopRuntime.GetModuleForMT(hnd.MethodTable);
                DesktopModule module     = DesktopRuntime.GetModule(moduleAddr);
                uint          token      = DesktopRuntime.GetMetadataToken(mt);

                bool isFree = mt == DesktopRuntime.FreeMethodTable;
                if (token == 0xffffffff && !isFree)
                {
                    return(null);
                }

                // Dynamic functions/modules
                uint tokenEnt = token;
                if (!isFree && (module == null || module.IsDynamic))
                {
                    tokenEnt = (uint)mt;
                }

                ModuleEntry modEnt = new ModuleEntry(module, tokenEnt);

                if (ret == null)
                {
                    IMethodTableData mtData = DesktopRuntime.GetMethodTableData(mt);
                    if (mtData == null)
                    {
                        return(null);
                    }

                    ret = new DesktopHeapType(() => GetTypeName(hnd, module, token), module, token, mt, mtData, this)
                    {
                        ComponentType = componentType
                    };
                    index = _types.Count;
                    ((DesktopHeapType)ret).SetIndex(index);
                    _indices[hnd]      = index;
                    _typeEntry[modEnt] = index;
                    _types.Add(ret);

                    Debug.Assert(_types[index] == ret);
                }
            }

            if (obj != 0 && ret.ComponentType == null && ret.IsArray)
            {
                ret.ComponentType = TryGetComponentType(obj, cmt);
            }

            return(ret);
        }