public static PermissionSetSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            var signature = new PermissionSetSignature()
            {
                StartOffset = reader.Position
            };

            var signatureHeader = reader.ReadByte();

            if (signatureHeader != '.')
            {
                throw new ArgumentException("Signature doesn't refer to a valid permission set signature.");
            }

            uint attributeCount;

            if (!reader.TryReadCompressedUInt32(out attributeCount))
            {
                return(signature);
            }

            for (int i = 0; i < attributeCount; i++)
            {
                signature.Attributes.Add(SecurityAttributeSignature.FromReader(header, reader));
            }
            return(signature);
        }
        /// <summary>
        /// Reads a single security attribute at the current position of the provided stream reader.
        /// </summary>
        /// <param name="image">The image the signature resides in.</param>
        /// <param name="reader">The reader to use.</param>
        /// <returns>The read attribute.</returns>
        public static SecurityAttributeSignature FromReader(MetadataImage image, IBinaryStreamReader reader)
        {
            var signature = new SecurityAttributeSignature
            {
                TypeName = reader.ReadSerString(),
            };

            reader.ReadCompressedUInt32();

            if (!reader.TryReadCompressedUInt32(out uint argumentCount))
            {
                return(signature);
            }

            if (argumentCount == 0)
            {
                return(signature);
            }

            for (int i = 0; i < argumentCount; i++)
            {
                signature.NamedArguments.Add(CustomAttributeNamedArgument.FromReader(image, reader));
            }

            return(signature);
        }
        public void CreateSimpleSecurityDeclaration()
        {
            const string typeNamespace = "System.WitchCraft";
            const string typeName = "MagicalWand";

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

            // create temp type.
            var type = new TypeDefinition(typeNamespace, typeName);
            type.MetadataRow.Column5 = 1; // FieldList
            type.MetadataRow.Column6 = 2; // MethodList.
            typeTable.Add(type);

            // create attribute.
            var securityAttribute = new SecurityAttributeSignature()
            {
                TypeName = typeof(TypeDescriptorPermissionAttribute).AssemblyQualifiedName,
            };

            // create permission set.
            var permissionSet = new PermissionSetSignature();
            permissionSet.Attributes.Add(securityAttribute);

            // create declaration.
            var declaration = new SecurityDeclaration(SecurityAction.Assert, permissionSet);
            type.SecurityDeclarations.Add(declaration);
            declarationTable.Add(declaration);

            assembly = Utilities.RebuildNetAssembly(assembly);
            tableStream = assembly.NetDirectory.MetadataHeader.GetStream<TableStream>();
            typeTable = tableStream.GetTable<TypeDefinition>();
            type = typeTable.First(x => x.IsTypeOf(typeNamespace, typeName));

            Assert.IsTrue(type.SecurityDeclarations.Count > 0);
            var newDeclaration = type.SecurityDeclarations[0];

            Assert.AreEqual(declaration.Action, newDeclaration.Action);
            Assert.AreEqual(declaration.PermissionSet.Attributes.Count, newDeclaration.PermissionSet.Attributes.Count);

            for (int i = 0; i < declaration.PermissionSet.Attributes.Count; i++)
            {
                var attribute = declaration.PermissionSet.Attributes[i];
                var newAttribute = newDeclaration.PermissionSet.Attributes[i];

                Assert.AreEqual(attribute.TypeName, newAttribute.TypeName);

            }
        }
        public static SecurityAttributeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            var signature = new SecurityAttributeSignature()
            {
                StartOffset = reader.Position,
                TypeName = reader.ReadSerString(),
            };

            reader.ReadCompressedUInt32();

            uint argumentCount;
            if (!reader.TryReadCompressedUInt32(out argumentCount))
                return signature;

            if (argumentCount == 0)
                return signature;

            for (int i = 0; i < argumentCount; i++)
                signature.NamedArguments.Add(CustomAttributeNamedArgument.FromReader(header, reader));

            return signature;
        }