| Severity | Error |
| Reported by | Metalama.Patterns.Observability |
| Message | The '<Property>' <Kind> is virtual, which is not supported by the [Observable] aspect. Remove the 'virtual' modifier, or exclude the <Kind> from the aspect with the [NotObservable] attribute. |
Cause
The [Observable] aspect rewrites the properties of the class so that their setters raise the PropertyChanged event, and it analyzes the dependencies between properties to notify computed properties. A derived class could override a virtual property with a different implementation, which would bypass the notifications and invalidate the dependency analysis. Therefore, the aspect doesn't support virtual properties.
Metalama reports LAMA5154 on each virtual property that the aspect processes, that is, the automatic properties of the class and the properties that other properties depend on. In the message, <Kind> is the kind of declaration and <Property> is the property.
In the following example, the Email property is virtual.
1using Metalama.Patterns.Observability;
2
3namespace Doc.LAMA5154.Error;
4
5[Observable]
6public partial class Customer
7{
8 public string? Name { get; set; }
9
10 // Error: the [Observable] aspect doesn't support virtual properties.
Error LAMA5154: The 'Customer.Email' property is virtual, which is not supported by the [Observable] aspect. Remove the 'virtual' modifier, or exclude the property from the aspect with the [NotObservable] attribute.
11 public virtual string? Email { get; set; }
12}
13
Resolution
Do one of the following:
- Remove the
virtualmodifier, as in the following example. - If the property doesn't need change notifications, exclude it from the aspect with the [NotObservable] attribute.
1using Metalama.Patterns.Observability;
2
3namespace Doc.LAMA5154.Fixed;
4
5[Observable]
6public partial class Customer
7{
8 public string? Name { get; set; }
9
10 // Fixed: the property is no longer virtual.
11 public string? Email { get; set; }
12}
13
1using System.ComponentModel;
2using Metalama.Patterns.Observability;
3
4namespace Doc.LAMA5154.Fixed;
5
6[Observable]
7public partial class Customer : INotifyPropertyChanged
8{
9 private string? _name;
10
11 public string? Name
12 {
13 get
14 {
15 return _name;
16 }
17
18 set
19 {
20 if (!object.ReferenceEquals(value, _name))
21 {
22 _name = value;
23 OnPropertyChanged("Name");
24 }
25 }
26 }
27
28 private string? _email;
29
30 // Fixed: the property is no longer virtual.
31 public string? Email
32 {
33 get
34 {
35 return _email;
36 }
37
38 set
39 {
40 if (!object.ReferenceEquals(value, _email))
41 {
42 _email = value;
43 OnPropertyChanged("Email");
44 }
45 }
46 }
47
48 protected virtual void OnPropertyChanged(string propertyName)
49 {
50 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
51 }
52
53 public event PropertyChangedEventHandler? PropertyChanged;
54}
55