// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Linq;
namespace CommunityToolkit.Mvvm.ComponentModel;
///
/// An attribute that can be used to support in generated properties. When this attribute is
/// used, the generated property setter will also call (or the equivalent
/// method in the target class) for the properties specified in the attribute data. This can be useful to keep the code compact when
/// there are one or more dependent properties that should also be reported as updated when the value of the annotated observable
/// property is changed. If this attribute is used in a field without , it is ignored.
///
/// In order to use this attribute, the containing type has to implement the interface
/// and expose a method with the same signature as . If the containing
/// type also implements the interface and exposes a method with the same signature as
/// , then this method will be invoked as well by the property setter.
///
///
/// This attribute can be used as follows:
///
/// partial class MyViewModel : ObservableObject
/// {
/// [ObservableProperty]
/// [AlsoNotifyChangeFor(nameof(FullName))]
/// private string name;
///
/// [ObservableProperty]
/// [AlsoNotifyChangeFor(nameof(FullName))]
/// private string surname;
///
/// public string FullName => $"{Name} {Surname}";
/// }
///
///
/// And with this, code analogous to this will be generated:
///
/// partial class MyViewModel
/// {
/// public string Name
/// {
/// get => name;
/// set
/// {
/// if (SetProperty(ref name, value))
/// {
/// OnPropertyChanged(nameof(FullName));
/// }
/// }
/// }
///
/// public string Surname
/// {
/// get => surname;
/// set
/// {
/// if (SetProperty(ref surname, value))
/// {
/// OnPropertyChanged(nameof(FullName));
/// }
/// }
/// }
/// }
///
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public sealed class AlsoNotifyChangeForAttribute : Attribute
{
///
/// Initializes a new instance of the class.
///
/// The name of the property to also notify when the annotated property changes.
public AlsoNotifyChangeForAttribute(string propertyName)
{
PropertyNames = new[] { propertyName };
}
///
/// Initializes a new instance of the class.
///
/// The name of the property to also notify when the annotated property changes.
///
/// The other property names to also notify when the annotated property changes. This parameter can optionally
/// be used to indicate a series of dependent properties from the same attribute, to keep the code more compact.
///
public AlsoNotifyChangeForAttribute(string propertyName, params string[] otherPropertyNames)
{
PropertyNames = new[] { propertyName }.Concat(otherPropertyNames).ToArray();
}
///
/// Gets the property names to also notify when the annotated property changes.
///
public string[] PropertyNames { get; }
}