PostSharp ignores all the types that have [CompilerGenerated]
attribute applied when performing the multicasting of the aspect attributes. This functionality is by design and is needed to avoid the application of the aspects on all the types generated by the C# compiler.
The generated types represent the implementation details of the C# compiler and applying the attributes by default would expose these implementation details to the user.
However, PostSharp treats generated methods in non-generated types with less restriction. These are the methods where the user normally expects to apply the aspects by default.
For example, the automatic property accessors are marked as [CompilerGenerated]
. You can control this behavior by setting the flags MulticastAttributes.CompilerGenerated
and MulticastAttributes.UserGenerated
on the AttributeTargetTypeAttributes property of your aspect.
If you need to apply the aspect to compiler-generated types, then you can do it by implementing your own aspect provider and applying it on the assembly level.
[MulticastAttributeUsage(MulticastTargets.Assembly)]
public class SampleAspectProvider : MulticastAttribute, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
var myAspect = new MyAspect();
var assembly = (Assembly) targetElement;
foreach (var type in assembly.GetTypes())
{
if (/* type is a valid target */)
{
foreach (var methodInfo in type.GetMethods())
{
yield return new AspectInstance(methodInfo, myAspect);
}
}
}
}
}
And apply in the target assembly:
[assembly: SampleAspectProvider]