Beispiel #1
0
        private MemberInfo GetMemberRef(int index, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            if (memberRefs == null)
            {
                memberRefs = new MemberInfo[MemberRef.records.Length];
            }
            if (memberRefs[index] == null)
            {
                int    owner = MemberRef.records[index].Class;
                int    sig   = MemberRef.records[index].Signature;
                string name  = GetString(MemberRef.records[index].Name);
                switch (owner >> 24)
                {
                case MethodDefTable.Index:
                    return(GetMethodAt(null, (owner & 0xFFFFFF) - 1));

                case ModuleRefTable.Index:
                    memberRefs[index] = ResolveTypeMemberRef(ResolveModuleType(owner), name, ByteReader.FromBlob(blobHeap, sig));
                    break;

                case TypeDefTable.Index:
                case TypeRefTable.Index:
                    memberRefs[index] = ResolveTypeMemberRef(ResolveType(owner), name, ByteReader.FromBlob(blobHeap, sig));
                    break;

                case TypeSpecTable.Index:
                {
                    Type type = ResolveType(owner, genericTypeArguments, genericMethodArguments);
                    if (type.IsArray)
                    {
                        MethodSignature methodSig = MethodSignature.ReadSig(this, ByteReader.FromBlob(blobHeap, sig), new GenericContext(genericTypeArguments, genericMethodArguments));
                        return(type.FindMethod(name, methodSig)
                               ?? universe.GetMissingMethodOrThrow(type, name, methodSig));
                    }
                    else if (type.IsConstructedGenericType)
                    {
                        MemberInfo member = ResolveTypeMemberRef(type.GetGenericTypeDefinition(), name, ByteReader.FromBlob(blobHeap, sig));
                        MethodBase mb     = member as MethodBase;
                        if (mb != null)
                        {
                            member = mb.BindTypeParameters(type);
                        }
                        FieldInfo fi = member as FieldInfo;
                        if (fi != null)
                        {
                            member = fi.BindTypeParameters(type);
                        }
                        return(member);
                    }
                    else
                    {
                        return(ResolveTypeMemberRef(type, name, ByteReader.FromBlob(blobHeap, sig)));
                    }
                }

                default:
                    throw new BadImageFormatException();
                }
            }
            return(memberRefs[index]);
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
 internal override ByteReader GetBlob(int blobIndex)
 {
     return(ByteReader.FromBlob(blobHeap, blobIndex));
 }
 internal ByteReader GetStandAloneSig(int index)
 {
     return(ByteReader.FromBlob(blobHeap, StandAloneSig.records[index]));
 }
Beispiel #5
0
        private MemberInfo GetMemberRef(int index, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            if (memberRefs == null)
            {
                memberRefs = new MemberInfo[MemberRef.records.Length];
            }
            if (memberRefs[index] == null)
            {
                int    owner = MemberRef.records[index].Class;
                int    sig   = MemberRef.records[index].Signature;
                string name  = GetString(MemberRef.records[index].Name);
                switch (owner >> 24)
                {
                case MethodDefTable.Index:
                    return(GetMethodAt(null, (owner & 0xFFFFFF) - 1));

                case ModuleRefTable.Index:
                    memberRefs[index] = ResolveTypeMemberRef(ResolveModuleType(owner), name, ByteReader.FromBlob(blobHeap, sig), genericTypeArguments, genericMethodArguments);
                    break;

                case TypeDefTable.Index:
                case TypeRefTable.Index:
                    memberRefs[index] = ResolveTypeMemberRef(ResolveType(owner), name, ByteReader.FromBlob(blobHeap, sig), genericTypeArguments, genericMethodArguments);
                    break;

                case TypeSpecTable.Index:
                    return(ResolveTypeMemberRef(ResolveType(owner, genericTypeArguments, genericMethodArguments), name, ByteReader.FromBlob(blobHeap, sig), genericTypeArguments, genericMethodArguments));

                default:
                    throw new BadImageFormatException();
                }
            }
            return(memberRefs[index]);
        }
Beispiel #6
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));
            }
        }