예제 #1
0
        public uint FindField(uint tdToken, string szField, byte[] signature)
        {
            IntPtr hEnum = new IntPtr();
            uint   cDefs, maxcDefs = 10;

            uint[] fieldDefs = new uint[maxcDefs];

            importer.EnumFields(ref hEnum, tdToken, fieldDefs, maxcDefs, out cDefs);
            while (cDefs > 0)
            {
                for (int i = 0; i < cDefs; i++)
                {
                    FieldProps props = GetFieldProps(fieldDefs[i]);
                    if (props.fName == szField && checkSignature(signature, props.sigBlob))
                    {
                        importer.CloseEnum(hEnum);
                        return(fieldDefs[i]);
                    }
                }
                importer.EnumMethods(ref hEnum, tdToken, fieldDefs, maxcDefs, out cDefs);
            }
            importer.CloseEnum(hEnum);
            return(0);
        }
예제 #2
0
        /// <summary>
        /// Registers any metadata token from tables
        /// (MemberRef,UserString,TypeRef,Method,TypeDef,Field,ModuleRef,AssemblyRef)
        /// from metadata scope A to metadata scope B.
        /// </summary>
        /// <param name="token">Token in metadata scope A.(new assembly)</param>
        /// <returns>Token in metadata scope B.(running assembly)</returns>
        public MetadataToken TranslateToken(MetadataToken cecToken)
        {
            uint          token = cecToken.ToUInt32();
            MetadataToken new_token;

            // Look in cache first
            if (cache.TryGetValue(token, out new_token))
            {
                return(new_token);
            }
            switch (cecToken.TokenType)
            {
            case TokenType.MemberRef:
                new_token = registerMemberRef(cecToken);
                break;

            case TokenType.String:
            {
                uint   nToken;
                string str = NewImporter.GetUserString(token);
                OldEmitter.CorMetaDataEmit.DefineUserString(str, (uint)str.Length, out nToken);
                new_token = new MetadataToken(nToken);
            }
            break;

            case TokenType.TypeRef:
                new_token = registerTypeRef(cecToken);
                break;

            case TokenType.Method:
            {
                if (addedMethods.TryGetValue(token, out new_token))
                {
                    break;
                }
                string name; uint classTk; byte[] signature; uint attr; uint rva; uint flags;
                NewImporter.GetMethodProps(token, out name, out classTk, out attr, out signature, out rva, out flags);
                MetadataToken classToken = TranslateToken(new MetadataToken(classTk));
                Signature     sig        = new Signature(signature);
                sig.Migrate(this);
                signature = sig.Compress();
                new_token = new MetadataToken(OldImporter.FindMethod(classToken.ToUInt32(), name, signature));
            }
            break;

            case TokenType.Field:
            {
                FieldProps fProps    = NewImporter.GetFieldProps(token);
                uint       newMClass = TranslateToken(new MetadataToken(fProps.mClass)).ToUInt32();
                fProps.sigBlob = Signature.Migrate(fProps.sigBlob, this, 1);
                //metadata.OldEmitter.CorMetaDataEmit.DefineMemberRef(newMClass,fProps.fName,fProps.sigBlob,(uint)fProps.sigBlob.Length,out new_token);
                uint nCorToken = OldImporter.FindField(newMClass, fProps.fName, fProps.sigBlob);
                if (nCorToken == 0)
                {
                    OldEmitter.CorMetaDataEmit.DefineField(newMClass, fProps.fName, fProps.pdwAttr, fProps.sigBlob, (uint)fProps.sigBlob.Length, fProps.pdwCPlusTypeFlag, fProps.ppValue, (uint)fProps.ppValue.Length, out nCorToken);
                }
                new_token = new MetadataToken(nCorToken);
            }
            break;

            case TokenType.TypeDef:
                TypeDefProps tProps = NewImporter.GetTypeDefProps(token);
                new_token = new MetadataToken(OldImporter.FindTypeDef(tProps.tName));
                break;

            case TokenType.ModuleRef:
            {
                uint   nToken;
                string name = NewImporter.GetModuleRefProps(token);
                OldEmitter.CorMetaDataEmit.DefineModuleRef(name, out nToken);
                new_token = new MetadataToken(nToken);
            }
            break;

            case TokenType.AssemblyRef:
            {
                int index = manager.ResourceManager.NewAssembly.Modules[0].AssemblyReferences.FindIndex(delegate(AssemblyNameReference asmRef){
                        return(asmRef.MetadataToken.ToUInt32() == token);
                    });
                string scopeName = manager.ResourceManager.NewAssembly.Modules[0].AssemblyReferences[index].FullName;

                index = manager.ResourceManager.OldAssembly.Modules[0].AssemblyReferences.FindIndex(delegate(AssemblyNameReference asmRef){
                        return(asmRef.FullName == scopeName);
                    });
                if (index == -1)
                {
                    throw new TranslatingException("Assembly reference not found, maybe it wasn't in used in original version");
                }
                else
                {
                    new_token = manager.ResourceManager.OldAssembly.Modules[0].AssemblyReferences[index].MetadataToken;
                }
            }
            break;

            case TokenType.TypeSpec:
                new_token = registerTypeSpec(cecToken);
                break;

            case TokenType.MethodSpec:
                uint            new_token_cor;
                MethodSpecProps props = newImporter.GetMethodSpecProps(token);
                props.tkParent  = TranslateToken(new MetadataToken(props.tkParent)).ToUInt32();
                props.signature = Signature.Migrate(props.signature, this);
                OldEmitter.CorMetaDataEmit.DefineMethodSpec(props.tkParent, props.signature, (uint)props.signature.Length, out new_token_cor);
                new_token = new MetadataToken(new_token_cor);
                break;

            case TokenType.Property:
            default:
                throw new NotImplementedException();
            }
            // Add to cache
            cache.Add(token, new_token);
            return(new_token);
        }