Open sandboxFocusImprove this doc

LAMA5203: An existing member has the name of the member to introduce

Severity Error
Reported by Metalama.Patterns.Wpf
Message The name of the existing <Kind> '<Member>', defined in or inherited by the type '<DeclaringType>', conflicts with the <IntroducedKind> name determined by the '<NamingConvention>' naming convention. Rename the existing <Kind> or change the naming convention.

Cause

The WPF aspects introduce a member whose name comes from a naming convention:

  • The [Command] aspect introduces a command property. With the default naming convention, a method named Save, ExecuteSave, or SaveAsync gets a property named SaveCommand.
  • The [DependencyProperty] aspect introduces a static registration field that holds the DependencyProperty object. For a property named Foo, the field is named FooProperty, as WPF requires.

Before introducing the member, the aspect checks whether the type already has a member or a nested type with that name, including members inherited from base types. If it does, Metalama reports LAMA5203 and the aspect isn't applied to the target. A typical case is a hand-written SaveCommand property left in the class after adding [Command] to the Save method, or a hand-written FooProperty field after adding [DependencyProperty] to the Foo property.

In the message, <Kind> and <Member> are the kind and the name of the existing member, <DeclaringType> is the type that contains the aspect's target, <IntroducedKind> is command property or registration field, and <NamingConvention> is the name of the naming convention that determined the name, for example default or explicitly-configured.

In the following example, the class declares a QuantityProperty field by hand, but the aspect also needs to introduce a field with this name for the Quantity dependency property.

1using Metalama.Patterns.Wpf;
2using System.Windows;
3
4namespace Doc.LAMA5203.Error;
5
6public partial class ProductControl : DependencyObject
7{
8    [DependencyProperty]
    Error LAMA5203: The name of the existing field 'ProductControl.QuantityProperty', defined in or inherited by the type 'ProductControl', conflicts with the registration field name determined by the 'default' naming convention. Rename the existing field or change the naming convention.

9    public int Quantity { get; set; }
10
11    // Error: this field has the name of the registration field that the aspect must introduce.
12    public static readonly DependencyProperty QuantityProperty =
13        DependencyProperty.Register( "Quantity", typeof(int), typeof(ProductControl) );
14}
15

Resolution

  • If the existing member is a hand-written implementation of the command or the dependency property, remove it: the aspect generates it.
  • Otherwise, rename the existing member.
  • For a command, you can also change the name of the generated property: rename the target method, or add a naming convention with a different CommandPropertyName pattern through the ConfigureCommand fabric extension method. Alternatively, set the CommandPropertyName property of the attribute.
  • For a dependency property, keep the WPF convention FooProperty whenever possible, because WPF tools and XAML rely on it. Renaming the C# property also renames the field. If you must use another name, set the RegistrationField property of the [DependencyProperty] attribute.

In the following example, the hand-written field is removed and the aspect introduces QuantityProperty.

Source Code
1using Metalama.Patterns.Wpf;
2using System.Windows;
3
4namespace Doc.LAMA5203.Fixed;
5
6public partial class ProductControl : DependencyObject
7{
8    // Fixed: the aspect introduces the QuantityProperty field.
9    [DependencyProperty]
10    public int Quantity { get; set; }
11}
12
Transformed Code
1using Metalama.Patterns.Wpf;
2using System.Windows;
3
4namespace Doc.LAMA5203.Fixed;
5
6public partial class ProductControl : DependencyObject
7{
8    // Fixed: the aspect introduces the QuantityProperty field.
9    [DependencyProperty]
10    public int Quantity
11    {
12        get
13        {
14            return (int)GetValue(QuantityProperty);
15        }
16
17        set
18        {
19            this.SetValue(QuantityProperty, value);
20        }
21    }
22
23    public static readonly DependencyProperty QuantityProperty;
24
25    static ProductControl()
26    {
27        QuantityProperty = DependencyProperty.Register("Quantity", typeof(int), typeof(ProductControl));
28    }
29}
30

See also