Caution
Undo/Redo was deprecated in PostSharp 2026.0 and will not be maintained. The API will be removed in the future.
To make an object usable for undo/redo operations, you will need to add the RecordableAttribute aspect to the class. This aspect instruments changes to fields and records them into a Recorder. The aspect also instruments public methods to group field changes into logical operations.
Making the class recordable
Add the
PostSharp.Patterns.Modelpackage to the project.Add the RecordableAttribute to the class.
Annotate your object model for parent/child relationships as explained in Annotating an Object Model for Parent/Child Relationships (Aggregatable).
Example
The following example shows an object model that has been prepared for undo/redo.
[Recordable]
public class Invoice
{
public Invoice()
{
this.Lines = new AdvisableCollection<InvoiceLine>();
}
[Reference]
public Customer Customer { get; set; }
[Child]
public IList<InvoiceLine> Lines { get; private set; }
[Child]
public Address DeliveryAddress { get; set; }
[NotRecorded]
public bool Persisted { get; set; }
}
[Recordable]
public class InvoiceLine
{
[Reference]
private Product product;
public decimal Amount { get; set; }
[Parent]
public Invoice ParentInvoice { get; private set; }
}
[Recordable]
public class Address
{
}
[Recordable]
public class Customer
{
}
By adding the RecordableAttribute aspect to the classes, all modifications to properties of these classes (including modifications to child collections) will be recorded.
Excluding a property from undo/redo
To exclude a property from participating in undo/redo, annotate that property with NotRecordedAttribute.
[Recordable]
public class Document
{
public string Text { get; set; }
[NotRecorded]
public bool AnyChangesMade { get; set; }
}