Example #1
0
        internal override Type ResolveType(int metadataToken, IGenericContext context)
        {
            int index = (metadataToken & 0xFFFFFF) - 1;

            if (index < 0)
            {
                throw TokenOutOfRangeException(metadataToken);
            }
            else if ((metadataToken >> 24) == TypeDefTable.Index && index < TypeDef.RowCount)
            {
                PopulateTypeDef();
                return(typeDefs[index]);
            }
            else if ((metadataToken >> 24) == TypeRefTable.Index && index < TypeRef.RowCount)
            {
                if (typeRefs == null)
                {
                    typeRefs = new Type[TypeRef.records.Length];
                }
                if (typeRefs[index] == null)
                {
                    int scope = TypeRef.records[index].ResolutionScope;
                    switch (scope >> 24)
                    {
                    case AssemblyRefTable.Index:
                    {
                        Assembly assembly = ResolveAssemblyRef((scope & 0xFFFFFF) - 1);
                        TypeName typeName = GetTypeName(TypeRef.records[index].TypeNameSpace, TypeRef.records[index].TypeName);
                        typeRefs[index] = assembly.ResolveType(typeName);
                        break;
                    }

                    case TypeRefTable.Index:
                    {
                        Type     outer    = ResolveType(scope, null);
                        TypeName typeName = GetTypeName(TypeRef.records[index].TypeNameSpace, TypeRef.records[index].TypeName);
                        typeRefs[index] = outer.ResolveNestedType(typeName);
                        break;
                    }

                    case ModuleTable.Index:
                    case ModuleRefTable.Index:
                    {
                        Module module;
                        if (scope >> 24 == ModuleTable.Index)
                        {
                            if (scope == 0 || scope == 1)
                            {
                                module = this;
                            }
                            else
                            {
                                throw new NotImplementedException("self reference scope?");
                            }
                        }
                        else
                        {
                            module = ResolveModuleRef(ModuleRef.records[(scope & 0xFFFFFF) - 1]);
                        }
                        TypeName typeName = GetTypeName(TypeRef.records[index].TypeNameSpace, TypeRef.records[index].TypeName);
                        typeRefs[index] = module.FindType(typeName) ?? module.universe.GetMissingTypeOrThrow(module, null, typeName);
                        break;
                    }

                    default:
                        throw new NotImplementedException("ResolutionScope = " + scope.ToString("X"));
                    }
                }
                return(typeRefs[index]);
            }
            else if ((metadataToken >> 24) == TypeSpecTable.Index && index < TypeSpec.RowCount)
            {
                if (typeSpecs == null)
                {
                    typeSpecs = new Type[TypeSpec.records.Length];
                }
                Type type = typeSpecs[index];
                if (type == null)
                {
                    TrackingGenericContext tc = context == null ? null : new TrackingGenericContext(context);
                    type = Signature.ReadTypeSpec(this, ByteReader.FromBlob(blobHeap, TypeSpec.records[index]), tc);
                    if (tc == null || !tc.IsUsed)
                    {
                        typeSpecs[index] = type;
                    }
                }
                return(type);
            }
            else
            {
                throw TokenOutOfRangeException(metadataToken);
            }
        }
Example #2
0
        internal Type ResolveType(int metadataToken, IGenericContext context)
        {
            switch (metadataToken >> 24)
            {
            case TypeDefTable.Index:
                PopulateTypeDef();
                return(typeDefs[(metadataToken & 0xFFFFFF) - 1]);

            case TypeRefTable.Index:
            {
                if (typeRefs == null)
                {
                    typeRefs = new Type[TypeRef.records.Length];
                }
                int index = (metadataToken & 0xFFFFFF) - 1;
                if (typeRefs[index] == null)
                {
                    int scope = TypeRef.records[index].ResolutionScope;
                    switch (scope >> 24)
                    {
                    case AssemblyRefTable.Index:
                    {
                        Assembly assembly = ResolveAssemblyRef((scope & 0xFFFFFF) - 1);
                        TypeName typeName = GetTypeName(TypeRef.records[index].TypeNameSpace, TypeRef.records[index].TypeName);
                        typeRefs[index] = assembly.ResolveType(typeName);
                        break;
                    }

                    case TypeRefTable.Index:
                    {
                        Type     outer    = ResolveType(scope, null);
                        TypeName typeName = GetTypeName(TypeRef.records[index].TypeNameSpace, TypeRef.records[index].TypeName);
                        typeRefs[index] = outer.ResolveNestedType(typeName);
                        break;
                    }

                    case ModuleTable.Index:
                        if (scope != 0 && scope != 1)
                        {
                            throw new NotImplementedException("self reference scope?");
                        }
                        typeRefs[index] = FindType(GetTypeName(TypeRef.records[index].TypeNameSpace, TypeRef.records[index].TypeName));
                        break;

                    case ModuleRefTable.Index:
                    {
                        Module   module   = ResolveModuleRef(ModuleRef.records[(scope & 0xFFFFFF) - 1]);
                        TypeName typeName = GetTypeName(TypeRef.records[index].TypeNameSpace, TypeRef.records[index].TypeName);
                        Type     type     = module.FindType(typeName);
                        if (type == null)
                        {
                            throw new TypeLoadException(String.Format("Type '{0}' not found in module '{1}'", typeName, module.Name));
                        }
                        typeRefs[index] = type;
                        break;
                    }

                    default:
                        throw new NotImplementedException("ResolutionScope = " + scope.ToString("X"));
                    }
                }
                return(typeRefs[index]);
            }

            case TypeSpecTable.Index:
            {
                if (typeSpecs == null)
                {
                    typeSpecs = new Type[TypeSpec.records.Length];
                }
                int  index = (metadataToken & 0xFFFFFF) - 1;
                Type type  = typeSpecs[index];
                if (type == null)
                {
                    TrackingGenericContext tc = context == null ? null : new TrackingGenericContext(context);
                    type = Signature.ReadTypeSpec(this, ByteReader.FromBlob(blobHeap, TypeSpec.records[index]), tc);
                    if (tc == null || !tc.IsUsed)
                    {
                        typeSpecs[index] = type;
                    }
                }
                return(type);
            }

            default:
                throw new NotImplementedException(String.Format("0x{0:X}", metadataToken));
            }
        }