Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostSharp OnMethodBoundaryAspect

Tags:

c#

postsharp

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.

like image 707
José F. Romaniello Avatar asked Nov 01 '25 19:11

José F. Romaniello


1 Answers

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

like image 134
Gael Fraiteur Avatar answered Nov 03 '25 09:11

Gael Fraiteur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!