Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INPC003 should not warn here #146

Open
JohanLarsson opened this issue Aug 23, 2019 · 1 comment
Open

INPC003 should not warn here #146

JohanLarsson opened this issue Aug 23, 2019 · 1 comment

Comments

@JohanLarsson
Copy link
Collaborator

JohanLarsson commented Aug 23, 2019

namespace ValidCode.Wrapping
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    public class WrappingProperties : INotifyPropertyChanged
    {
        private WithProperties withProperties = new WithProperties();

        public event PropertyChangedEventHandler? PropertyChanged;

        public int P1
        {
            get => this.withProperties.P1;
            set
            {
                if (value == this.withProperties.P1)
                {
                    return;
                }

                this.withProperties.P1 = value;
                this.OnPropertyChanged();
            }
        }

        public int P2
        {
            get => this.withProperties.P2;
#pragma warning disable INPC003 // Notify when property changes.
            set => this.TrySet(ref this.withProperties, new WithProperties { P1 = this.P1, P2 = value });
#pragma warning restore INPC003 // Notify when property changes.
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool TrySet<T>(ref T field, T newValue, [CallerMemberName] string? propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, newValue))
            {
                return false;
            }

            field = newValue;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        private class WithProperties
        {
            public int P1 { get; set; }

            public int P2 { get; set; }
        }
    }
}
@JohanLarsson
Copy link
Collaborator Author

It can be smarter there but probably not a super common case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant