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 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; }