我应用了这个方面:
[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);
}
}
超过某些namespace
的课程:
[assembly: WmiClassMethodAspect(AttributeTargetTypes = "OperatingSystemsWmi.*",
AttributePriority = 10, ApplyToStateMachine = false,
AttributeTargetElements = MulticastTargets.Method)]
但如果它不尊重AttributeTargetElements
设置:它也会输入WmiClassMethodAspect.OnEntry
和WmiClassMethodAspect.OnExit
的属性(set_MyProperty
和get_MyProperty
,实例)
答案 0 :(得分:2)
这是因为 MulticastTargets.Method 将属性访问器视为方法(它们是)。注意, MulticastTarget.Property 指定属性(方法组)用于例如 LocationLevelAspect
要产生预期的行为,您需要根据名称添加属性getter和setter的排除项:
[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)]
编辑: 如果您想在一个属性中使用它,则可以使用正则表达式:
[assembly: WmiClassMethodAspect(AttributeTargetTypes = "OperatingSystemsWmi.*",
AttributePriority = 10, ApplyToStateMachine = false,
AttributeTargetElements = MulticastTargets.Method,
AttributeTargetMembers = "regex:^(?!get_|set_).+")]
这可能是最简洁的解决方案。