Skip to content

Assembly wide AttributeDecorator

Reflection Emit edited this page Sep 26, 2018 · 3 revisions

The assembly wide attribute decorator makes it possible to decorate classes, methods and properties with an attribute during build. The decorator respects the AttributeUsage and the InterceptionRule of the specified attribute.
By describing certain properties of the class, method or property for instance name, modificator, parameters, it is possible to decorate hundreds of members during build. The removal of the attribute is also easy - by deactivating the attribute in the configuration file.

Getting started

To enable this feature you have to add a json file to the root folder of your project. Linked files are currently not supported. The filename of the json file has to end with cauldron.fody.json.
It is also possible to have multiple json files with different configuration in the folder.

The json file

[
  {
    // If true, the decorators decribed in decorator
    // are only applied if the constant "DEBUG" is set.
    // Default is false.
    "debug-only": false,
    // Activates / deactivates the decorators.
    // Default is true.
    "is-active": false,
    // A collection of decorators with its associated filter.
    "decorator": [
      {
        // The full name of the attribute
        // The attribute itself has to reside in the referenced assemblies.
        "type-name": "System.Runtime.InteropServices.GuidAttribute",
        // The parameters that is passed to the attibute.
        "parameters": [
          {
            // The parameter's type name
            "type-name": "System.String",
            // The paraters's value
            "value": "08B13836-E085-4B10-A3E4-2AD81ECF4EBF"
          }
        ],
        // If true, classes are included in the search. Default is false.
        "decorate-class": true,
        // If true, methods are included in the search. Default is false.
        "decorate-methods": true,
        // If true, properties are included in the search. Default is false.
        "decorate-properties": true,
        // Specifies certain properties of the class for the filter.
        // This has no effect of decorate-class is false.
        "target-class-filter": {
            // Also include public classes. Default is true.
            "public": true,
            // Also include internal classes Default is true.
            "internal": true,
            // Also include private classes Default is true.
            "private": true,
            // Also include static classes Default is true.
            "static": true,
            // Also include instanced classes Default is true.
            "instanced": true,
            // The name of the class.
            // This accepts regex -> example: \\w*Controller will exclude all classes
            // with a name that does not end with Controller.
            // * is equivalent to [a-zA-Z].*
            //  Default is "*"
            "name": "*",
            // Only includes classes that implements all listed interfaces.
            // Default is "no interface restrictions"
            "requires-interfaces": [ "System.Collections.IEnumerable" ]
        },
        // Specifies certain properties of the method for the filter.
        "target-method-filter": {
            // Also include public methods. Default is true.
            "public": true,
            // Also include internal methods. Default is true.
            "internal": true,
            // Also include protected methods. Default is true.
            "protected": true,
            // Also include private methods. Default is true.
            "private": true,
            // Also include static methods. Default is true.
            "static": true,
            // Also include non-static methods. Default is true.
            "instanced": true,
            // The name of the method.
            // Accepts regex.
            // * is equivalent to [a-zA-Z].*
            // Default is "*"
            "name": "*", 
            // Only includes methods that returns the following type.
            // Default is empty. Empty means no return type filter.
            // To filter void methods, use System.Void.
            "return-types-name": null,
            // Only includes methods that matches the parameters list types.
            // Default is false.
            "parameter-match": true,
            // Only includes methods that strictly matches the parameter types.
            // Strict means that the type name has to exactly match; otherwise
            // the types are tested for assignability. Means if the parameter type
            // is System.Int32, then all methods with parameter type System.Int32 and
            // System.Object are included.
            // Default is false.
            "parameter-strict": false,
            // Only include methods that matches the listes parameter types.
            // Default is empty list which also means paramterless.
            "parameters": [ "System.Int32" ]
        },
        // Specifies certain properties of the property for the filter.
        "target-property-filter": {
            // Also include public properties. Default is true.
            "public": true,
            // Also include internal properties. Default is true.
            "internal": true,
            // Also include protected properties. Default is true.
            "protected": true,
            // Also include private properties. Default is true.
            "private": true,
            // Also include static properties. Default is true.
            "static": true,
            // Also include non-static properties. Default is true.
            "instanced": true,
            // The name of the property.
            // Accepts regex.
            // * is equivalent to [a-zA-Z].*
            // Default is "*".
            "name": "*", 
            // Only includes properties with type name.
            // Default is null. Empty means no return type filter.
            "return-types-name": [ "System.String" ]
        }
      }
    ]
  }
]

Generic type names

Generic type names are written with an grave accent ( ` ) followed by the number of generic parameters.
Example:

// Dictionary
Dictionary<,>              /* -> */      System.Collections.Generic.Dictionary`2
Dictionary<string, int>    /* -> */      System.Collections.Generic.Dictionary`2<System.String, System.Int32>
// IEnumerable
IEnumerable<>              /* -> */      System.Collections.Generic.IEnumerable`1
IEnumerable<object>        /* -> */      System.Collections.Generic.IEnumerable`1<System.Object>

Excluded types and unsupported filters

  • Types that inherits from Attribute
  • Types named <Module>
  • Parameter filter does not support arrays (yet)