AttributeTargetElements = MulticastTargets.Method isn't respected?

雨燕双飞 提交于 2020-01-16 01:19:29

问题


I applied this aspect:

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method)] // regular methods, excluding ctors!
public class WmiClassMethodAspect : OnMethodBoundaryAspect
{
    public WmiClassMethodAspect() { ApplyToStateMachine = false; } // PostSharp Express edition...

    public override void OnEntry(MethodExecutionArgs args)
    {
        //base.OnEntry(args);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        //base.OnExit(args);
    }
}

over certain namespace's class:

[assembly: WmiClassMethodAspect(AttributeTargetTypes = "OperatingSystemsWmi.*", 
    AttributePriority = 10, ApplyToStateMachine = false, 
    AttributeTargetElements = MulticastTargets.Method)]

But if it doesn't respect the AttributeTargetElements setting: it enters WmiClassMethodAspect.OnEntry and WmiClassMethodAspect.OnExit for properties too (set_MyProperty and get_MyProperty, for instance)


回答1:


This happens because MulticastTargets.Method treats property accessors as methods (which they are). Note that MulticastTarget.Property specifies property (method group) for purposes of e.g. LocationLevelAspect.

To produce the intended behavior, you would need to add an exclusion for property getters and setters based their name:

[assembly: WmiClassMethodAspect(AttributeTargetTypes = "OperatingSystemsWmi.*", 
           AttributePriority = 10, AttributeTargetElements = MulticastTargets.Method, 
           AttributeTargetMembers = "get_*", AttributeExclude = true)]

[assembly: WmiClassMethodAspect(AttributeTargetTypes = "OperatingSystemsWmi.*", 
           AttributePriority = 10, ApplyToStateMachine = false, AttributeTargetElements = MulticastTargets.Method, 
           AttributeTargetMembers = "set_*", AttributeExclude = true)]

EDIT: If you want this in one attribute, you can use regular expressions:

[assembly: WmiClassMethodAspect(AttributeTargetTypes = "OperatingSystemsWmi.*", 
           AttributePriority = 10, ApplyToStateMachine = false, 
           AttributeTargetElements = MulticastTargets.Method,
           AttributeTargetMembers = "regex:^(?!get_|set_).+")]

This is probably the most succinct solution.



来源:https://stackoverflow.com/questions/28965548/attributetargetelements-multicasttargets-method-isnt-respected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!