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.ImportMethod(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);
}