AttributeTargetElements = MulticastTargets.Method 不被尊重?

AttributeTargetElements = MulticastTargets.Method isn't respected?

我应用了这方面:

[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 的 class:

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

但如果它不遵守 AttributeTargetElements 设置:它也会为属性输入 WmiClassMethodAspect.OnEntryWmiClassMethodAspect.OnExitset_MyPropertyget_MyProperty,因为实例)

发生这种情况是因为 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_).+")]

这可能是最简洁的解决方案。