开发者

PostSharp OnMethodBoundaryAspect

开发者 https://www.devze.com 2022-12-24 04:11 出处:网络
I\'m working on an aspect with postsharp 1.5 and OnMethodBoundaryAspect. I want my aspect have the following behavior by default:

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 bec开发者_开发知识库aue 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.


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


Another possible approach is to use the IAspectProvider, which provide aspects dynamically.

sample for creating such aspect, that inject different aspects for regular methods and constructors can be found in the PostSharp training about Aspect Providers.

In your case you can use the sample provided and based on the 'ReflectedType.IsPublic' condition decide which aspect you wish to inject.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号