Search code examples
c#postsharp

PostSharp OnMethodBoundaryAspect


I'm working on an aspect with postsharp 1.5 and OnMethodBoundaryAspect. I want my aspect have the following behavior by default:

1-If the attribute is used at class level the aspect is applied only on PUBLIC methods.

2-The user of the aspect can put the aspect in a private or protected method.

If I use this [MulticastAttributeUsage( MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Public)] the point 1 works, but the case 2 doesn't even build becaue is incompatible.

Then I tried to use: AttributeTargetTypeAttributes = MulticastAttributes.Public; in the constructor of the aspect, but doesn't work.

Thank you very much in advance.


Solution

  • Unfortunately, there is no way to implement your requirements using a SINGLE aspect class.

    You may use three aspect class:

    public abstract class MyAspect : OnMethodBoundaryAspect
    {
    }
    
    [MulticastAttributeUsage(..., 
          TargetMembersAttributes = MulticastAttributes.Public )]
    [AttributeUsage(AttributeTargets.Class)]
    public class ClassLevelAspect : MyAspect
    {
    }
    
    [MulticastAttributeUsage(..., 
         TargetMembersAttributes = MulticastAttributes.NonPublic )]
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodLevelAspect : MyAspect
    {
    }
    

    -gael