This project is read-only.

Working with ICustomAttribute from the PE reader

Topics: Metadata Model, PE Writer
Jan 4, 2010 at 10:54 PM

I'm attempting to use CCI for creating a code generator, by iterating over a set of assemblies, discovering the types and their metadata and then generating the code. I would like to be able to control the code generation by attaching custom attributes to the metadata of the original types.

Something like:

[GenerateSpecialClass(true, "foo", IsReallySpecial=false)]
public class MyClass { ... }

I've been able to get much of the way through the metadata with relative ease (the MetadataHelper types and methods were extremely helpful). However, I have an INamedTypeDefinition and get an IEnumerable<ICustomAttribute> from the Attributes property. From here, I can't figure out what to do to get the value of custom attribute and it's properties.

Could someone show me, given an ICustomAttribute, how I can retrieve the values from my example attribute. Assume it's definition is:

public GenericSpecialClassAttribute : Attribute
public bool Prop1 { get; set; }
public string Prop2 {get; set; }
public bool IsReallySpecial {get; set; }
public GenericSpecialClassAttribute(bool prop1, string prop2)
Prop1 = prop1;
Prop2 = prop2;


Any help would be very much appreciated. I assume I cast this to some other interface and do something magical on it; but I couldn't find a helper that did anything with it and don't fully understand the implementation/model hierarchy.

Jan 5, 2010 at 7:08 PM

The values you are looking for are hiding away in the Arguments property of the attribute. See the methods in AttributeHelper.cs for some more examples of using attributes.