PostSharp.ILAdding Aspects to CodeAdding Aspects Declaratively Using Attributes
Open sandboxFocusImprove this doc

Adding Aspects Declaratively Using Attributes

In .NET, you normally need to write one line of code for any application of a target attribute. If a custom attribute applies to all types in a namespace, you have to manually add the custom attribute to every single type.

By contrast, multicast custom attributes allow you to apply a custom attribute on multiple declarations from a single line of code by using wildcards or regular expressions, or by filtering on some attributes. It makes it easy to apply an aspect to, say, all public static methods of a namespace, with a single line of code.

Multicast attributes can be inherited: you can put an attribute on an interface and ask it to apply to all classes implementing this interface. Attribute inheritance also works for classes, virtual or interface methods, and parameters of virtual or interface methods.

Custom attributes supporting multicasting need to be derived from MulticastAttribute. All PostSharp aspects and constraints are derived from this class.

Note

Multicasting of custom attribute is a feature of PostSharp. If you do not transform your assembly using PostSharp, multicast attributes will behave as plain old custom attributes.

Note

This documentation often refers to this as "quoteInline" multicasting and inheritance. This is not totally accurate. Although this feature has been developed to support aspects, you can use it for your own custom attributes, even if they are not aspects. To use multicasting and inheritance for custom attributes that are not aspects, simply derive the attribute class from MulticastAttribute instead of Attribute.

Attribute multicasting supports the following scenarios:

Reference

MulticastAttribute
MulticastAttributeUsageAttribute
IAspectProvider
Other Resources

Overriding and Removing Aspect Instances
Reflecting Aspect Instances at Runtime