12

When I have a class that I declare implements then INotifyPropertyChanged interface, ReSharper will automatically generate this implementation:

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
  var handler = PropertyChanged;
  if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

which I am always editing to be this:

public event PropertyChangedEventHandler PropertyChanged = delegate { };

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
   PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

Can I somehow edit the autogenerated code? Resharper's documentation is less than clear to me on this.

Scott Baker
  • 10,013
  • 17
  • 56
  • 102
  • 3
    Why is it so important to remove thread safety? Copy > check > invike is a best practice. – Johan Larsson Jul 02 '15 at 23:05
  • 2
    I could understand wanting to change to C# 6 standards if you're using VS 2015, but the change described here is a poor change to make... – Reed Copsey Jul 02 '15 at 23:37
  • Could someone please explain to me how changing this code removes thread safety? I don't understand why this is a poor choice. Why do you need to copy/check/invoke when you _know_ the event has been defined and is _never_ null? Not being combative; I truly don't understand. – Scott Baker Jul 06 '15 at 16:27

1 Answers1

5

No, you can't edit the auto-generated code, because it needs to handle a number of possibilities when generating - e.g. C# 6 uses the ?. operator, and it also needs to handle when the event already exists and has already been initialised.

If you do want to use the shorthand version which doesn't have the local variable and the null check, then you can create the event first, and initialise it with = () => { }; before generating the OnPropertyChanged method. However, it is probably best to keep the local var + null check, for thread safety.

citizenmatt
  • 18,085
  • 5
  • 55
  • 60
  • I don't understand. I _am_ creating the event and initializing it with `= delegate {}` - basically the same thing. Can you elaborate on how this is not thread safe? – Scott Baker Jul 06 '15 at 16:43
  • 1
    Nope, you're right. The `delegate {}` version is thread safe. The issue with the local var + null check is to make sure there's an event handler to invoke, and there's a thread safety issue if you use the event directly. By ensuring there's at least one event handler, with the empty delegate, that thread safety concern is handled. – citizenmatt Jul 07 '15 at 09:34
  • I agree with your conclusion, but not your premise. That is what a customization is; Its not expected to handle every possibility. To use your example, if I create a customized propertyChanged template, then I clearly want it to use the exact syntax I've told it to use. It shouldn't matter if `?.` is added to the standard or not. I didn't tell it to use it. If, and when, I decided to update my customization or go back to the default, then it can use `?.`. @ScottSEA Unfortunately, as was stated, it is *not* an option in Resharper right now. However, I would submit it as a feature request. – Taekahn Jul 09 '15 at 17:30