简体   繁体   中英

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

These links show how to add solution-wide aspects:

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

My question : assuming added some solution-wide-aspect, how can I exclude certain assembly/class/method/whatever ?

AttributeExclude can be used to remove all other instances of the same attribute type from specified elements set.

AutoDataContractAttribute can be multicasted using PostSharp xml configuration file to all classes in MyNamespace.Customer :

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

Say the attribute should not be multicasted types in MyNamespace.Customer.Excluded namespace. This could be done by adding:

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

Whole example:

<?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>

When AttributeExclude is True then AttributePriority property needs to be specified as well. It is done automatically when using XML but in more complicated cases it could be not sufficient and you would need to specify the AttributePriority explicitly. It could be done by adding aspects declaratively using attributes :

[assembly: AutoDataContractAttribute(AttributePriority = 1,
    AttributeTargetTypes="MyNamespace.Customer")]
[assembly: AutoDataContractAttribute(AttributeExclude = true,
    AttributeTargetTypes = "MyNamespace.Customer.Excluded.*",
    AttributePriority = 10)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM