问题
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