Ejemplo n.º 1
0
        public void NewStandaloneSignature()
        {
            var module = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld);

            // Import arbitrary method signature.
            var importer  = new ReferenceImporter(module);
            var signature = new StandAloneSignature(
                importer.ImportMethodSignature(MethodSignature.CreateStatic(module.CorLibTypeFactory.Void)));

            // Ensure reference is added to the module by referencing it in main.
            var instructions = module.ManagedEntrypointMethod.CilMethodBody.Instructions;

            instructions.Insert(0, CilOpCodes.Ldnull);
            instructions.Insert(0, CilOpCodes.Calli, signature);

            // Rebuild.
            var builder = new ManagedPEImageBuilder();
            var result  = builder.CreateImage(module);

            // Assert valid token.
            var newToken = result.TokenMapping[signature];

            Assert.NotEqual(0u, newToken.Rid);

            // Assert token resolves to the same method reference.
            var newModule    = ModuleDefinition.FromImage(result.ConstructedImage);
            var newSignature = (StandAloneSignature)newModule.LookupMember(newToken);

            Assert.Equal(signature.Signature, newSignature.Signature, new SignatureComparer());
        }