Post­Sharp Documentation / Adding Aspects to Code / Adding Aspects Declaratively Using Attributes / Reflecting Aspect Instances at Runtime

Reflecting Aspect Instances at Runtime

Attribute multicasting has been primarily designed as a mechanism to add aspects to a program. Most of the time, the custom attribute representing an aspect can be removed after the aspect has been applied.

By default, if you add an aspect to a program and look at the resulting program using a disassembler or System.Reflection, you will not find these corresponding custom attributes.

If you need your aspect (or any other multicast attribute) to be reflected by System.Reflection or any other tool, you have to set the MulticastAttributeUsageAttribute.PersistMetaData property to true.

For instance:

C#
[MulticastAttributeUsage( MulticastTargets.Class, PersistMetaData = true )]
  public class TagAttribute : MulticastAttribute
  {
    public string Tag;
  }
Note Note

Multicasting of attributes is not limited only to PostSharp aspects. You can multicast any custom attribute in your codebase in the same way as shown here. If a custom attribute is multicast with the PersistMetaData property set to true, when reflected on the compiled code will look as if you had manually added the custom attribute in all of the locations.