AsmResolver.Net.Metadata.ReferenceImporter.ImportMethod C# (CSharp) Method

ImportMethod() public method

public ImportMethod ( MethodDefinition definition ) : IMemberReference
definition MethodDefinition
return IMemberReference
        public IMemberReference ImportMethod(MethodDefinition definition)
        {
            if (definition.Header == _tableStreamBuffer.StreamHeader.MetadataHeader)
                return definition;

            var table = _tableStreamBuffer.GetTable<MemberReference>();
            var newReference = table.FirstOrDefault(x => _signatureComparer.MatchMembers(x, definition));
            if (newReference == null)
            {
                newReference = new MemberReference(
                    ImportType(definition.DeclaringType),
                    definition.Name,
                    ImportMethodSignature(definition.Signature));
                table.Add(newReference);
            }
            return newReference;
        }

Same methods

ReferenceImporter::ImportMethod ( MethodBase method ) : MemberReference

Usage Example

コード例 #1
0
        public void AddStringReference()
        {
            const string testConstant = "Lorem ipsum.";

            // set up temp assembly.
            var assembly = Utilities.CreateTempNetAssembly();
            var tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
            var methodTable = tableStream.GetTable<MethodDefinition>();
            var importer = new ReferenceImporter(tableStream);

            // write code.
            var body = methodTable[0].MethodBody;
            body.Instructions.Clear();
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldstr, testConstant));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Call,
                importer.ImportMethod(typeof(Console).GetMethod("WriteLine", new Type[]
                    {
                        typeof(string)
                    }))));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Call,
                importer.ImportMethod(typeof(Console).GetMethod("ReadKey", Type.EmptyTypes))));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ret));

            // build and validate.
            assembly = Utilities.RebuildNetAssembly(assembly);
            methodTable = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>().GetTable<MethodDefinition>();

            var operand = methodTable[0].MethodBody.Instructions[0].Operand;
            Assert.IsInstanceOfType(operand, typeof(string));
            Assert.AreEqual(testConstant, operand);
        }
All Usage Examples Of AsmResolver.Net.Metadata.ReferenceImporter::ImportMethod