public unsafe void VerifyMultipleOptionalModifiers()
        {
            // Type 1: int32 modopt([mscorlib]System.Runtime.CompilerServices.IsLong) modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
            // Type 2: char*
            // Type 3: uint32
            // Type 4: char modopt([mscorlib]System.Runtime.CompilerServices.IsConst)*
            var testSignature = new byte[] { 0x20, 0x45, 0x20, 0x69, 0x08, 0x0F, 0x03, 0x09, 0x0F, 0x20, 0x55, 0x03 };
            var types         = new string[]
            {
                "int32 modopt(100001A) modopt(1000011)",
                "char*",
                "uint32",
                "char modopt(1000015)*"
            };

            fixed(byte *testSignaturePtr = &testSignature[0])
            {
                var signatureBlob = new BlobReader(testSignaturePtr, testSignature.Length);
                var provider      = new OpaqueTokenTypeProvider();
                var decoder       = new SignatureDecoder <string, DisassemblingGenericContext>(provider, metadataReader: null, genericContext: null);

                foreach (string typeString in types)
                {
                    // Verify that each type is decoded as expected
                    Assert.Equal(typeString, decoder.DecodeType(ref signatureBlob));
                }
                // And that nothing is left over to decode
                Assert.True(signatureBlob.RemainingBytes == 0);
                Assert.Throws <BadImageFormatException>(() => decoder.DecodeType(ref signatureBlob));
            }
        }
 [InlineData(new byte[] { 0x0A /*GENERICINST*/, 0 /*count*/ })] // no type parameters
 public unsafe void DecodeInvalidMethodSpecificationSignature(byte[] testSignature)
 {
     fixed(byte *testSignaturePtr = &testSignature[0])
     {
         var signatureBlob = new BlobReader(testSignaturePtr, testSignature.Length);
         var provider      = new OpaqueTokenTypeProvider();
         var decoder       = new SignatureDecoder <string, DisassemblingGenericContext>(provider, metadataReader: null, genericContext: null);
     }
 }
Пример #3
0
        [InlineData(new byte[] { 0x0A /*GENERICINST*/, 0 /*count*/ })] // no type parameters
        public unsafe void DecodeInvalidMethodSpecificationSignature(byte[] testSignature)
        {
            fixed(byte *testSignaturePtr = &testSignature[0])
            {
                var signatureBlob = new BlobReader(testSignaturePtr, testSignature.Length);
                var provider      = new OpaqueTokenTypeProvider();
                var decoder       = new SignatureDecoder <string>(provider);

                Assert.Throws <BadImageFormatException>(() => decoder.DecodeMethodSpecificationSignature(ref signatureBlob));
            }
        }
        public unsafe void DecodeValidMethodSpecificationSignature(string[] expectedTypes, byte[] testSignature)
        {
            fixed(byte *testSignaturePtr = &testSignature[0])
            {
                var signatureBlob = new BlobReader(testSignaturePtr, testSignature.Length);
                var provider      = new OpaqueTokenTypeProvider();
                var decoder       = new SignatureDecoder <string, DisassemblingGenericContext>(provider, metadataReader: null, genericContext: null);

                IEnumerable <string> actualTypes = decoder.DecodeMethodSpecificationSignature(ref signatureBlob);

                Assert.Equal(expectedTypes, actualTypes);
                Assert.True(signatureBlob.RemainingBytes == 0);
                Assert.Throws <BadImageFormatException>(() => decoder.DecodeType(ref signatureBlob));
            }
        }
        public void DecodeVarArgsDefAndRef()
        {
            using (FileStream stream = File.OpenRead(AssemblyPathHelper.GetAssemblyLocation(typeof(VarArgsToDecode).GetTypeInfo().Assembly)))
                using (var peReader = new PEReader(stream))
                {
                    MetadataReader       metadataReader = peReader.GetMetadataReader();
                    TypeDefinitionHandle typeDefHandle  = TestMetadataResolver.FindTestType(metadataReader, typeof(VarArgsToDecode));
                    TypeDefinition       typeDef        = metadataReader.GetTypeDefinition(typeDefHandle);
                    MethodDefinition     methodDef      = metadataReader.GetMethodDefinition(typeDef.GetMethods().First());

                    Assert.Equal("VarArgsCallee", metadataReader.GetString(methodDef.Name));
                    var provider = new OpaqueTokenTypeProvider();

                    MethodSignature <string> defSignature = methodDef.DecodeSignature(provider, null);
                    Assert.Equal(SignatureCallingConvention.VarArgs, defSignature.Header.CallingConvention);
                    Assert.Equal(1, defSignature.RequiredParameterCount);
                    Assert.Equal(new[] { "int32" }, defSignature.ParameterTypes);

                    int refCount = 0;
                    foreach (MemberReferenceHandle memberRefHandle in metadataReader.MemberReferences)
                    {
                        MemberReference memberRef = metadataReader.GetMemberReference(memberRefHandle);

                        if (metadataReader.StringComparer.Equals(memberRef.Name, "VarArgsCallee"))
                        {
                            Assert.Equal(MemberReferenceKind.Method, memberRef.GetKind());
                            MethodSignature <string> refSignature = memberRef.DecodeMethodSignature(provider, null);
                            Assert.Equal(SignatureCallingConvention.VarArgs, refSignature.Header.CallingConvention);
                            Assert.Equal(1, refSignature.RequiredParameterCount);
                            Assert.Equal(new[] { "int32", "bool", "string", "float64" }, refSignature.ParameterTypes);
                            refCount++;
                        }
                    }

                    Assert.Equal(1, refCount);
                }
        }