| Severity | Error |
| Reported by | Metalama.Patterns.Wpf |
| Message | Several members can be used as the <Role> for <TargetDescription>'<Target>' <AsRequired>by the '<NamingConvention>' naming convention. Rename or remove the members that must not be used, so that only one member matches. |
Cause
The [Command] and [DependencyProperty] aspects find their helper members by name: the can-execute method or property of a command, and the property-changed and validate methods of a dependency property. Each role must be filled by a single member.
Metalama reports LAMA5205 when more than one member with a valid signature matches the same role. The diagnostic is reported on each of these members, and the aspect isn't applied to the target. Typical cases are:
- For a command named
Save, the class has both a valid can-execute method and a valid can-execute property, for example aCanSave()method and anIsSaveEnabledproperty. The default naming convention accepts the namesCanExecuteSave,CanSave, andIsSaveEnabled, as methods or properties. - Several overloads of the helper method have a valid signature, for example
CanSave()andCanSave( object parameter ), orOnFooChanged()andOnFooChanged( int value ). - A custom naming convention uses a pattern, such as CanExecutePatterns, that matches several members.
In the message, <Role> is the role, <Target> is the aspect's target, and <NamingConvention> is the naming convention. <AsRequired> is as required when the member is required, for example because its name was set explicitly in the attribute.
In the following example, the Save command has two overloads of the CanSave method, and both have a valid signature.
1using Metalama.Patterns.Wpf;
2
3namespace Doc.LAMA5205.Error;
4
5public class Document
6{
7 public bool IsModified { get; set; }
8
9 [Command]
10 private void Save() { }
11
12 // Error: the overloads of CanSave are both valid can-execute methods.
Error LAMA5205: Several members can be used as the can-execute method for [Command] method 'Document.Save()' by the 'default' naming convention. Rename or remove the members that must not be used, so that only one member matches.
13 private bool CanSave() => this.IsModified;
14
Error LAMA5205: Several members can be used as the can-execute method for [Command] method 'Document.Save()' by the 'default' naming convention. Rename or remove the members that must not be used, so that only one member matches.
15 private bool CanSave( int parameter ) => parameter > 0;
16}
17
Resolution
Make sure that exactly one member matches each role:
- Remove the members that duplicate the logic, or rename the ones that aren't meant to be helpers of the aspect.
- Merge overloads of the helper method into a single method.
- For a command, name the intended member explicitly with CanExecuteMethod or CanExecuteProperty. Only methods or only properties are then considered, which removes an ambiguity between a method and a property.
- For a dependency property, name the intended method explicitly with PropertyChangedMethod or ValidateMethod. This doesn't help with overloads of the same name.
In the following example, the second overload of CanSave is removed, so that a single method matches.
1using Metalama.Patterns.Wpf;
2
3namespace Doc.LAMA5205.Fixed;
4
5public class Document
6{
7 public bool IsModified { get; set; }
8
9 [Command]
10 private void Save() { }
11
12 // Fixed: only one method matches the can-execute role.
13 private bool CanSave() => this.IsModified;
14}
15
1using Metalama.Patterns.Wpf;
2
3namespace Doc.LAMA5205.Fixed;
4
5public class Document
6{
7 public bool IsModified { get; set; }
8
9 [Command]
10 private void Save() { }
11
12 // Fixed: only one method matches the can-execute role.
13 private bool CanSave() => this.IsModified;
14
15 public Document()
16 {
17 SaveCommand = DelegateCommandFactory.CreateDelegateCommand(Save, CanSave);
18 }
19
20 public DelegateCommand SaveCommand { get; }
21}
22