none
How to rewrite this property to be more generic/fewer chars so will be easier to apply to hundreds of properties? RRS feed

  • Question

  • I have hundreds of properties that I want to record in a log when the value gets set. The properties look like this:

            private bool? ResponseSetTime
            {
                get { return _ResponseSetTime; }
                set
                {
                    _ResponseSetTime = value;
                    DebugLogFile.Info.WriteLine("{0}.{1}: {2}",
                        GetType().Name,
                        MethodBase.GetCurrentMethod().Name.Substring(4),
                        value);
                }
            }
            private bool? _ResponseSetTime = null;

    The GetType thing returns the class name.

    The MethodBase etc gets the property name and strips off the 'set_' part of the property.

    Given that there are hundreds of places where this DebugLogFile line needs to be added, is there a way that the property can be rewritten so as to reduce the amount of copy/paste and  keystrokes required to implement the logging?


    Richard Lewis Haggard


    Friday, November 1, 2019 3:34 PM

All replies

  • Maybe, you can use the property change notification.

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification

    Friday, November 1, 2019 4:52 PM
  • Have a look at Fody PropertyChanged.

    Example, given the following class

    public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        
        public string GivenNames { get; set; }
        public string FamilyName { get; set; }
        public string FullName => $"{GivenNames} {FamilyName}";
    }

    We get this when compiled

    public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        string givenNames;
        public string GivenNames
        {
            get => givenNames;
            set
            {
                if (value != givenNames)
                {
                    givenNames = value;
                    OnPropertyChanged(InternalEventArgsCache.GivenNames);
                    OnPropertyChanged(InternalEventArgsCache.FullName);
                }
            }
        }
    
        string familyName;
        public string FamilyName
        {
            get => familyName;
            set 
            {
                if (value != familyName)
                {
                    familyName = value;
                    OnPropertyChanged(InternalEventArgsCache.FamilyName);
                    OnPropertyChanged(InternalEventArgsCache.FullName);
                }
            }
        }
    
        public string FullName => $"{GivenNames} {FamilyName}";
    
        protected void OnPropertyChanged(PropertyChangedEventArgs eventArgs)
        {
            PropertyChanged?.Invoke(this, eventArgs);
        }
    }
    
    internal static class InternalEventArgsCache
    {
        internal static PropertyChangedEventArgs FamilyName = new PropertyChangedEventArgs("FamilyName");
        internal static PropertyChangedEventArgs FullName = new PropertyChangedEventArgs("FullName");
        internal static PropertyChangedEventArgs GivenNames = new PropertyChangedEventArgs("GivenNames");
    }

    For some examples

    http://www.shenchauhan.com/blog/2018/6/18/fody-inotifypropertychanged-the-easy-way

    http://dontcodetired.com/blog/post/Say-Goodbye-to-Boring-INotifyPropertyChanged-Implementation-in-Universal-Windows-Apps-with-Fody


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, November 2, 2019 12:18 AM
    Moderator