# Data Formats ## Input Data Formats ILLink uses several data formats to control or influence the trimming process. The data formats are not versioned but are backward compatible. - [Descriptors](#descriptor-format) - [Substitutions](#substitution-format) - [Custom Attributes Annotations](#custom-attributes-annotations-format) ## Output Data Formats - [Dependencies Trace](#dependencies-trace-format) # Format Details ## Descriptor Format Descriptors are used to direct the trimmer to always keep some items in the assembly, regardless of if the trimmer can find any references to them. Descriptor XML can be embedded in an assembly. In that case it must be stored as an embedded resource with logical name `ILLink.Descriptors.xml`. To achieve this when building an assembly use this in the project file to include the XML: ```xml ILLink.Descriptors.xml ``` Embedded descriptors only take effect if the containing assembly is included in the trimmer output, so if something from that assembly is marked to be kept. Descriptor XML can also be passed to the trimmer on the command via the [`-x` parameter](illink-options.md#trimming-from-an-xml-descriptor). ### XML Examples ### Preserve entire assembly ```xml ``` ### Preserve assembly using fully qualified name ```xml ``` ### Preserve a type The `required` attribute specifies that if the type is not marked, during the mark operation, it will not be trimmed. Both `required` and `preserve` can be combined together. ```xml ``` ### Preserve all methods or all fields on a type ```xml ``` ### Preserve more than one type within an assembly ```xml ``` ### Preserve only selected fields on a type ```xml ``` ### Preserve only selected methods on a type ```xml ``` ### Preserve only selected properties on type ```xml ``` ### Preserve only selected events on a type ```xml ``` ## Substitution Format Substitutions direct the trimmer to replace specific method's body with either a throw or return constant statements. Substitutions have effect only on assemblies which are trimmed with assembly action `link`, any other assembly will not be affected. That said it is possible to have a `copy` assembly with the substitution on a method in it, and then a separate `link` assembly which calls such method. The `link` assembly will see the constant value of the method after the substitution and potentially remove unused branches and such. Substitutions XML can be embedded in an assembly by including it as an embedded resource with logical name `ILLink.Substitutions.xml`. To include an XML file in an assembly this way, use this in the project file: ```xml ILLink.Substitutions.xml ``` Embedded substitutions only take effect if the containing assembly is included in the trimmer output. Embedded substitutions should only address methods from the containing assembly. Substitutions XML can be specified on the command line via the [`--substitutions` parameter](illink-options.md#using-custom-substitutions). Using substitutions with `ipconstprop` optimization (enabled by default) can help reduce output size as any dependencies under conditional logic which will be evaluated as unreachable will be removed. ### Substitute method body with a constant The `value` attribute is optional and only required when the method should be hardcoded to return a non-default value and the return type is not `void`. ```xml ``` ### Remove method Entire method body is replaces with `throw` instruction when method is referenced. ```xml ``` ### Override static field value with a constant The `initialize` attribute is optional and when not specified the code to set the static field to the value will not be generated. ```xml ``` ### Remove embedded resources ```xml ``` ### Conditional substitutions and descriptors The `feature` and `featurevalue` attributes are optional, but must be used together when used. They can be applied to any element to specify conditions under which the contained substitutions or descriptors are applied, based on feature settings passed via `--feature FeatureName bool` ```xml ``` `featuredefault="true"` can be used to indicate that this `featurevalue` is the default value for `feature`, causing the contained substitutions or descriptors to be applied even when the feature setting is not passed to the trimmer. Note that this will only have an effect where it is applied - the default value is not remembered or reused for other elements. ```xml ``` ## Custom Attributes Annotations Format Attribute annotations direct the trimmer to behave as if the specified item has the specified attribute. Attribute annotations can only be used to add attributes which have effect on trimmer behavior, all other attributes will be ignored. Attributes added via attribute annotations only influence trimmer behavior, they are never added to the output assembly. Attribute annotation XML can be embedded in an assembly by including it as an embedded resource with logical name `ILLink.LinkAttributes.xml`. To include an XML file in an assembly this way, use this in the project file: ```xml ILLink.LinkAttributes.xml ``` Embedded attribute annotations should only address methods from the containing assembly. Whereas attribute annotations specified on the command line via the [`--link-attributes` parameter](illink-options.md#supplementary-custom-attributes) can alter types and members in any assembly. The attribute element requires 'fullname' attribute without it trimmer will generate a warning and skip the attribute. Optionally you can use the 'assembly' attribute to point to certain assembly to look for the attribute, if not specified the trimmer will look up the attribute in any loaded assembly. Inside an attribute element in the xml you can further define argument, field and property elements used as an input for the attribute. An attribute can have several arguments, several fields or several properties. When writing custom attribute with multiple arguments you need to write the xml elements in an order-dependent form. That is, the first xml argument element corresponds to the first custom attribute argument, second xml argument element correspond to the second custom attribute argument and so on. When argument type is not specified it's considered to be of `string` type. Any other custom attribute value has to have its type specified for trimmer to find the correct constructor overload. ```xml StringValue -45 Sunday StringValue 200 ``` ### Custom attribute on assembly ```xml Argument ``` ### Custom attribute on type This allows to add a custom attribute to a class, interface, delegate, struct or enum. ```xml Argument ``` ### Custom attribute on type field ```xml DefaultConstructor ``` ### Custom attribute on property ```xml DefaultConstructor ``` ### Custom attribute on event ```xml DefaultConstructor ``` ### Custom attribute on method parameter ```xml DefaultConstructor PublicConstructors DefaultConstructor ``` ### Custom attribute in multiple method parameters ```xml DefaultConstructor DefaultConstructor PublicConstructors ``` ### Custom attribute on nested type ```xml DefaultConstructor ``` ### Custom attribute on type in all assemblies ```xml DefaultConstructor ``` ### Conditional custom attributes The `feature` and `featurevalue` attributes are optional, but must be used together when used. They can be applied to any element to specify conditions under which the contained custom attributes are applied. ```xml PublicConstructors ``` ### Custom attributes with feature ```xml ``` ### Removing custom attributes Any custom attribute can be annotated with a special custom attribute which can be used to specify that all instances of the attribute can be removed by the trimmer. To do this use `internal="RemoveAttributeInstances"` instead of specifying `fullname` in the attribute as described in the following example: ```xml ``` In some cases, it's useful to remove only specific usage of the attribute. This can be achieved by specifying the value or values of the arguments to match. In the example below only `System.Reflection.AssemblyMetadataAttribute` custom attributes with the first argument equal to `RemovableValue` will be removed. ```xml RemovableValue ``` Notice that a descriptor file containing the custom attribute type overrides this behavior. In case the custom attribute type is being referenced in a descriptor file and in the attribute annotations file for removal, the custom attribute will not be removed. ## Dependencies Trace Format This is the format of data used to capture trimmer logic about why members, types, and other metadata elements were marked by the trimmer as required and persisted in the trimmed output. The format includes edges of the graph for every dependency which was tracked by the trimmer.