如何从解决方案范围内排除某些 assembly/class/method/whatever?

How to exclude certain assembly/class/method/whatever from solution-wide-aspect?

这些链接显示了如何添加解决方案范围的方面:

  1. Adding Aspects Using XML
  2. Solution-Level Aspects and PostSharp Configuration Files
  3. Detecting Deadlocks at Runtime

我的问题:假设添加了一些解决方案范围的方面,我如何排除某些 assembly/class/method/whatever?

AttributeExclude 可用于从指定元素集中删除相同属性类型的所有其他实例。

可以使用 PostSharp xml 配置文件将 AutoDataContractAttribute 多播到 MyNamespace.Customer 中的所有 类:

<my:AutoDataContractAttribute AttributeTargetTypes="MyNamespace.Customer" />

说属性不应该是 MyNamespace.Customer.Excluded 命名空间中的多播类型。这可以通过添加来完成:

<my:AutoDataContractAttribute AttributeExclude="True" AttributeTargetTypes="MyNamespace.Customer.Excluded.*" />

整个例子:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
    <Multicast xmlns:my="clr-namespace:MyCustomAttributes;assembly:MyAssembly">
        <my:AutoDataContractAttribute AttributeTargetTypes="MyNamespace.Customer" />
        <my:AutoDataContractAttribute AttributeExclude="True" AttributeTargetTypes="MyNamespace.Customer.Excluded.*" />
    </Multicast>
</Project>

AttributeExclude 为 True 时,还需要指定 AttributePriority 属性。它在使用 XML 时自动完成,但在更复杂的情况下它可能还不够,您需要明确指定 AttributePriority。可以通过 使用属性 :

以声明方式添加方面来完成
[assembly: AutoDataContractAttribute(AttributePriority = 1,
    AttributeTargetTypes="MyNamespace.Customer")]
[assembly: AutoDataContractAttribute(AttributeExclude = true,
    AttributeTargetTypes = "MyNamespace.Customer.Excluded.*",
    AttributePriority = 10)]