internal CustomAttribute(MetadataImage image, MetadataRow <uint, uint, uint> row) : base(row.MetadataToken) { var tableStream = image.Header.GetStream <TableStream>(); _parent = new LazyValue <IHasCustomAttribute>(() => { var parentToken = tableStream.GetIndexEncoder(CodedIndex.HasCustomAttribute).DecodeIndex(row.Column1); IMetadataMember member; return(image.TryResolveMember(parentToken, out member) ? (IHasCustomAttribute)member : null); }); _constructor = new LazyValue <ICustomAttributeType>(() => { var ctorToken = tableStream.GetIndexEncoder(CodedIndex.CustomAttributeType).DecodeIndex(row.Column2); IMetadataMember member; return(image.TryResolveMember(ctorToken, out member) ? (ICustomAttributeType)member : null); }); _signature = new LazyValue <CustomAttributeSignature>(() => CustomAttributeSignature.FromReader( this, tableStream.MetadataHeader.GetStream <BlobStream>().CreateBlobReader(row.Column3), true)); }
public CustomAttribute(ICustomAttributeType constructor, CustomAttributeSignature signature) : base(new MetadataToken(MetadataTokenType.CustomAttribute)) { _parent = new LazyValue <IHasCustomAttribute>(); _constructor = new LazyValue <ICustomAttributeType>(constructor); _signature = new LazyValue <CustomAttributeSignature>(signature); }
public void CustomAttributeToken_Create() { byte[] content = new byte[] { 0x01, 0x00 }; CustomAttributeSignature token = new CustomAttributeSignature(content); Assert.AreEqual(1, token.Tokens.Count); }
public CustomAttributeSignature ReadCustomAttributeSignature(CustomAttribute parent, uint signature) { CustomAttributeSignature customAttrSig = null; BlobSignatureReader reader; if (TryGetBlobReader(signature, out reader)) { using (reader) { ushort sign = reader.ReadUInt16(); if (sign != 0x0001) { throw new ArgumentException("Signature doesn't refer to a valid Custom Attribute signature"); } int fixedArgCount = 0; if (parent.Constructor.Signature != null && parent.Constructor.Signature.Parameters != null) { fixedArgCount = parent.Constructor.Signature.Parameters.Length; } CustomAttributeArgument[] fixedArgs = new CustomAttributeArgument[fixedArgCount]; bool canReadNamedArgs = true; // temporary solution for skipping named args when fixed args failed. for (int i = 0; i < fixedArgCount; i++) { fixedArgs[i] = new CustomAttributeArgument(reader.ReadCustomAttributeArgumentValue(parent.Constructor.Signature.Parameters[i].ParameterType)); if (fixedArgs[i].Value == null) { canReadNamedArgs = false; } } CustomAttributeArgument[] namedArgs = null; if (!reader.EndOfStream && canReadNamedArgs) { int namedArgCount = reader.ReadUInt16(); namedArgs = new CustomAttributeArgument[namedArgCount]; for (int i = 0; i < namedArgCount; i++) { byte argSignature = reader.ReadByte(); TypeReference argType = reader.ReadCustomAttributeFieldOrPropType(); string name = reader.ReadUtf8String(); namedArgs[i] = new CustomAttributeArgument(reader.ReadCustomAttributeArgumentValue(argType), name, argSignature == 0x53 ? CustomAttributeArgumentType.NamedField : CustomAttributeArgumentType.NamedProperty); } } customAttrSig = new CustomAttributeSignature(fixedArgs, namedArgs); } } return(customAttrSig); }
public void CreateSimpleAttribute() { // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem; var tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); var fieldTable = tableStream.GetTable <FieldDefinition>(); var attributeTable = tableStream.GetTable <CustomAttribute>(); var importer = new ReferenceImporter(tableStream); // create field. var field = new FieldDefinition("MyField", FieldAttributes.Public | FieldAttributes.Static, new FieldSignature(typeSystem.String)); fieldTable.Add(field); // create custom attribute. var signature = new CustomAttributeSignature(); signature.FixedArguments.Add(new CustomAttributeArgument(typeSystem.String, new ElementSignature("Lorem ipsum dolor sit amet."))); signature.FixedArguments.Add(new CustomAttributeArgument(typeSystem.Boolean, new ElementSignature(true))); var attribute = new CustomAttribute(importer.ImportMember(typeof(ObsoleteAttribute).GetConstructor(new Type[] { typeof(string), typeof(bool) })), signature); field.CustomAttributes.Add(attribute); attributeTable.Add(attribute); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly); tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); fieldTable = tableStream.GetTable <FieldDefinition>(); field = fieldTable[0]; attributeTable = tableStream.GetTable <CustomAttribute>(); var newAttribute = attributeTable[0]; Assert.IsTrue(field.CustomAttributes.Contains(newAttribute)); Assert.AreEqual(attribute.Constructor.FullName, newAttribute.Constructor.FullName); Assert.AreEqual(attribute.Signature.FixedArguments.Count, newAttribute.Signature.FixedArguments.Count); for (int i = 0; i < attribute.Signature.FixedArguments.Count; i++) { Assert.AreEqual(attribute.Signature.FixedArguments[i].Elements[0].Value, newAttribute.Signature.FixedArguments[i].Elements[0].Value); } }
public void CreateAttributeWithEnumArgument() { // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); var assemblyTable = tableStream.GetTable <AssemblyDefinition>(); var attributeTable = tableStream.GetTable <CustomAttribute>(); var importer = new ReferenceImporter(tableStream); var assemblyDef = assemblyTable[0]; // create custom attribute. var signature = new CustomAttributeSignature(); signature.FixedArguments.Add( new CustomAttributeArgument(importer.ImportTypeSignature(typeof(DebuggableAttribute.DebuggingModes)), new ElementSignature((int)DebuggableAttribute.DebuggingModes.Default))); var attribute = new CustomAttribute(importer.ImportMember(typeof(DebuggableAttribute).GetConstructor(new Type[] { typeof(DebuggableAttribute.DebuggingModes) })), signature); assemblyDef.CustomAttributes.Add(attribute); attributeTable.Add(attribute); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly); tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); assemblyTable = tableStream.GetTable <AssemblyDefinition>(); assemblyDef = assemblyTable[0]; attributeTable = tableStream.GetTable <CustomAttribute>(); var newAttribute = attributeTable[0]; Assert.IsTrue(assemblyDef.CustomAttributes.Contains(newAttribute)); Assert.AreEqual(attribute.Constructor.FullName, newAttribute.Constructor.FullName); Assert.AreEqual(attribute.Signature.FixedArguments.Count, newAttribute.Signature.FixedArguments.Count); for (int i = 0; i < attribute.Signature.FixedArguments.Count; i++) { Utilities.ValidateArgument(attribute.Signature.FixedArguments[i], newAttribute.Signature.FixedArguments[i]); } }
internal CustomAttribute(MetadataHeader header, MetadataToken token, MetadataRow <uint, uint, uint> row) : base(header, token, row) { var tableStream = header.GetStream <TableStream>(); _parent = new LazyValue <IHasCustomAttribute>(() => { var parentToken = tableStream.GetIndexEncoder(CodedIndex.HasCustomAttribute).DecodeIndex(row.Column1); return(parentToken.Rid != 0 ? (IHasCustomAttribute)tableStream.ResolveMember(parentToken) : null); }); _constructor = new LazyValue <ICustomAttributeType>(() => { var ctorToken = tableStream.GetIndexEncoder(CodedIndex.CustomAttributeType).DecodeIndex(row.Column2); return(ctorToken.Rid != 0 ? (ICustomAttributeType)tableStream.ResolveMember(ctorToken) : null); }); _signature = new LazyValue <CustomAttributeSignature>(() => CustomAttributeSignature.FromReader(this, Header.GetStream <BlobStream>().CreateBlobReader(MetadataRow.Column3))); }
private void CloneCustomAttributes(IHasCustomAttribute source, IHasCustomAttribute newOwner) { foreach (var attribute in source.CustomAttributes) { var signature = new CustomAttributeSignature(); foreach (var argument in attribute.Signature.FixedArguments) { signature.FixedArguments.Add(CloneAttributeArgument(argument)); } foreach (var argument in attribute.Signature.NamedArguments) { signature.NamedArguments.Add(new CustomAttributeNamedArgument(argument.ArgumentMemberType, _importer.ImportTypeSignature(argument.ArgumentType), argument.MemberName, CloneAttributeArgument(argument.Argument))); } var newAttribute = new CustomAttribute((ICustomAttributeType)_importer.ImportReference(attribute.Constructor), signature); newOwner.CustomAttributes.Add(newAttribute); } }
public CustomAttributeSignature ReadCustomAttributeSignature(CustomAttribute parent, uint signature) { CustomAttributeSignature customAttrSig = null; using (BlobSignatureReader reader = GetBlobReader(signature)) { ushort sign = reader.ReadUInt16(); if (sign != 0x0001) { throw new ArgumentException("Signature doesn't refer to a valid Custom Attribute signature"); } int fixedArgCount = 0; if (parent.Constructor.Signature != null && parent.Constructor.Signature.Parameters != null) { fixedArgCount = parent.Constructor.Signature.Parameters.Length; } CustomAttributeArgument[] fixedArgs = new CustomAttributeArgument[fixedArgCount]; for (int i = 0; i < fixedArgCount; i++) { fixedArgs[i] = new CustomAttributeArgument(ReadArgumentValue(reader, parent.Constructor.Signature.Parameters[i].ParameterType)); } int namedArgCount = 0; CustomAttributeArgument[] namedArgs = new CustomAttributeArgument[namedArgCount]; customAttrSig = new CustomAttributeSignature(fixedArgs, namedArgs); } return(customAttrSig); }
/// <summary> /// Creates a new custom attribute. /// </summary> /// <param name="constructor">The constructor of the attribute to call.</param> /// <param name="signature">The signature containing the arguments to the constructor.</param> public CustomAttribute(ICustomAttributeType constructor, CustomAttributeSignature signature) : this(new MetadataToken(TableIndex.CustomAttribute, 0)) { Constructor = constructor; Signature = signature; }
public void CreateAttributeWithNamedArgument() { const string fieldName = "MyField"; const string argumentValue = "MyXmlAttribute"; const string propertyName = "IsNullable"; const bool propertyValue = true; // set up temp assembly. var assembly = Utilities.CreateTempNetAssembly(); var typeSystem = assembly.NetDirectory.MetadataHeader.TypeSystem; var tableStream = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>(); var fieldTable = tableStream.GetTable <FieldDefinition>(); var attributeTable = tableStream.GetTable <CustomAttribute>(); var importer = new ReferenceImporter(tableStream); // create temp field. var field = new FieldDefinition(fieldName, FieldAttributes.Static, new FieldSignature(typeSystem.String)); fieldTable.Add(field); // create custom attribute. var signature = new CustomAttributeSignature(); signature.FixedArguments.Add( new CustomAttributeArgument(typeSystem.String, new ElementSignature(argumentValue))); signature.NamedArguments.Add( new CustomAttributeNamedArgument() { ArgumentMemberType = CustomAttributeArgumentMemberType.Property, ArgumentType = typeSystem.Boolean, MemberName = propertyName, Argument = new CustomAttributeArgument(typeSystem.Boolean, new ElementSignature(propertyValue)) }); var attribute = new CustomAttribute(importer.ImportMember(typeof(XmlAttributeAttribute).GetConstructor(new Type[] { typeof(string) })), signature); field.CustomAttributes.Add(attribute); attributeTable.Add(attribute); // build and validate. assembly = Utilities.RebuildNetAssembly(assembly); fieldTable = tableStream.GetTable <FieldDefinition>(); field = fieldTable.First(x => x.Name == fieldName); attributeTable = tableStream.GetTable <CustomAttribute>(); var newAttribute = attributeTable[0]; Assert.IsTrue(field.CustomAttributes.Contains(newAttribute)); Assert.AreEqual(attribute.Constructor.FullName, newAttribute.Constructor.FullName); Assert.AreEqual(attribute.Signature.FixedArguments.Count, newAttribute.Signature.FixedArguments.Count); for (int i = 0; i < attribute.Signature.FixedArguments.Count; i++) { Utilities.ValidateArgument(attribute.Signature.FixedArguments[i], newAttribute.Signature.FixedArguments[i]); } Assert.AreEqual(attribute.Signature.NamedArguments.Count, newAttribute.Signature.NamedArguments.Count); for (int i = 0; i < attribute.Signature.NamedArguments.Count; i++) { Utilities.ValidateNamedArgument(attribute.Signature.NamedArguments[i], newAttribute.Signature.NamedArguments[i]); } }
/// <inheritdoc /> protected override CustomAttributeSignature GetSignature() { return(CustomAttributeSignature.FromReader(_parentModule, Constructor, _parentModule.DotNetDirectory.Metadata.GetStream <BlobStream>().GetBlobReaderByIndex(_row.Value))); }