Ejemplo n.º 1
0
        /// <summary>
        /// Translate new method to other metadata scope.
        /// </summary>
        /// <param name="methodDef"><c>MethodDefintion</c> of method to be translated.</param>
        /// <param name="newMethodDef"><c>MethodDefintion</c> of the result. This instance is used only as information holder and
        /// does not contain IL code of new method.</param>
        /// <returns>Description of translated method.</returns>
        public MethodDescriptor TranslateNewMethod(MethodDefinition methodDef, out MethodDefinition newMethodDef)
        {
            MethodDescriptor log;

            newMethodDef = new MethodDefinition(methodDef.Name, methodDef.Attributes,
                                                metadata.FindTypeReference(Builder.Manager.ResourceManager.OldAssembly, methodDef.ReturnType.Scope.Name, methodDef.ReturnType.FullName));

            log.srcToken = methodDef.MetadataToken.ToUInt32();

            // build IL header

            log.localVarsToken = buildILHeader(methodDef);

            newMethodDef.Body.LocalVarToken = new MetadataToken(log.localVarsToken);
            copyParameters(methodDef, newMethodDef);
            // Add instructions

            processBody(methodDef);

            // Emit method to metadata
            string methodName;
            uint   pClass, pdwAttr, pdwImplFlags, RVA;

            byte[] signature;

            metadata.NewImporter.GetMethodProps(methodDef.MetadataToken.ToUInt32(), out methodName,
                                                out pClass, out pdwAttr, out signature, out RVA, out pdwImplFlags);

            MetadataToken token = metadata.TranslateToken(new MetadataToken(pClass));

            signature = Signature.Migrate(signature, metadata);

            metadata.OldEmitter.CorMetaDataEmit.DefineMethod(pClass, methodName, pdwAttr, signature,
                                                             (uint)signature.Length, 0, pdwImplFlags, out log.destToken);

            newMethodDef.MetadataToken = new MetadataToken(log.destToken);

            log.newRva = 0;
            log.codeIL = buffer.ToArray();
            buffer.Clear();

            return(log);
        }