There are times where you cannot or don't want to add custom attributes manually.
For instance, you cannot add any custom attribute to an auto-generated field backing a XAML element. The WPF designer generates C# code from your XAML files and stores them in i.g.cs files hidden in the obj folder. You cannot modify these files directly.
Another scenario is when you have a large amount of fields and don't want to annotate each of them individually.
Field rules allow you to annotate a field as a child or a reference programmatically, without adding a custom attribute to each field manually.
In the following example, let's consider a class where PostSharp shows an error message * COM002: “Field/property InvoiceLine.product must be annotated with a custom attribute [Child], [Reference] or [Parent].” *
[Aggregatable]
public class InvoiceLine
{
private Product product;
public decimal Amount { get; set; }
}
To automatically mark all fields as references by default:
Create a class inherited from the FieldRule class.
public class RefererenceFieldRule : FieldRule
Override the GetRelationshipInfo(FieldInfo) method. PostSharp calls the GetRelationshipInfo(FieldInfo) method for each field that is not annotated with the ChildAttribute, ReferenceAttribute or ParentAttribute custom attribute. The GetRelationshipInfo(FieldInfo) method allows you to specify the field relationship by returning a RelationshipInfo instance.
public override RelationshipInfo? GetRelationshipInfo(FieldInfo field) { return new RelationshipInfo(RelationshipKind.None, RelationshipKind.Reference); }
Decorate your project’s assembly with a RegisterFieldRuleAttribute custom attribute to activate your field rule.
[assembly: RegisterFieldRule(typeof(RefererenceFieldRule))]
Caution
You have to mark each project assembly to make your FieldRule active in the whole solution.
Note
PostSharp has two built-in rules: one rule for auto-generated WinForms fields and one rule for auto-generated XAML fields.
<Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Button x:Name="myButton" /> </Grid> </Window>
In this example, the WPF designer generates a myButton
field to a MainWindow
class. Both the myButton
field and the MainWindow
class are indirectly inherited from System.Windows.Controls.Control. The built-in rule annotates the myButton
field as a reference automatically.
See Also
Reference