예제 #1
0
        public void PersistentNamedArgument()
        {
            var assembly = NetAssemblyFactory.CreateAssembly(DummyAssemblyName, true);
            var header   = assembly.NetDirectory.MetadataHeader;
            var image    = header.LockMetadata();
            var importer = new ReferenceImporter(image);

            var namedArg = new CustomAttributeNamedArgument(CustomAttributeArgumentMemberType.Property,
                                                            image.TypeSystem.Boolean, "Exclude",
                                                            new CustomAttributeArgument(image.TypeSystem.Boolean, new ElementSignature(true)));

            var customAttribute = new CustomAttribute(
                (ICustomAttributeType)importer.ImportMethod(
                    typeof(ObfuscationAttribute).GetConstructor(Type.EmptyTypes)),
                new CustomAttributeSignature {
                NamedArguments = { namedArg }
            });

            image.Assembly.CustomAttributes.Add(customAttribute);

            header.UnlockMetadata();

            image = header.LockMetadata();
            Assert.Single(image.Assembly.CustomAttributes);
            var newArg = image.Assembly.CustomAttributes[0].Signature.NamedArguments[0];

            Assert.Equal(namedArg.MemberName, newArg.MemberName);
            Assert.Equal(namedArg.ArgumentMemberType, newArg.ArgumentMemberType);
            Assert.Equal(namedArg.Argument.ArgumentType, newArg.Argument.ArgumentType, _comparer);
            Assert.Equal(namedArg.Argument.Elements[0].Value, newArg.Argument.Elements[0].Value);
        }
예제 #2
0
 public static void ValidateNamedArgument(CustomAttributeNamedArgument originalArgument,
                                          CustomAttributeNamedArgument argument)
 {
     Assert.Equal(originalArgument.ArgumentMemberType, argument.ArgumentMemberType);
     Assert.Equal(originalArgument.MemberName, argument.MemberName);
     ValidateType(originalArgument.ArgumentType, argument.ArgumentType);
     ValidateArgument(originalArgument.Argument, argument.Argument);
 }
 public static CustomAttributeNamedArgument FromReader(MetadataHeader header, IBinaryStreamReader reader)
 {
     var signature = new CustomAttributeNamedArgument
     {
         StartOffset = reader.Position,
         ArgumentMemberType =
             (reader.CanRead(sizeof (byte))
                 ? (CustomAttributeArgumentMemberType)reader.ReadByte()
                 : CustomAttributeArgumentMemberType.Field),
         ArgumentType = TypeSignature.ReadFieldOrPropType(header, reader),
         MemberName = reader.ReadSerString(),
     };
     signature.Argument = CustomAttributeArgument.FromReader(header, signature.ArgumentType, reader);
     return signature;
 }
예제 #4
0
 public static void ValidateNamedArgument(CustomAttributeNamedArgument originalArgument,
     CustomAttributeNamedArgument argument)
 {
     Assert.AreEqual(originalArgument.ArgumentMemberType, argument.ArgumentMemberType);
     Assert.AreEqual(originalArgument.MemberName, argument.MemberName);
     ValidateType(originalArgument.ArgumentType, argument.ArgumentType);
     ValidateArgument(originalArgument.Argument, argument.Argument);
 }