Vedo due tipi di implementazione di INotifyPropertyChanged
Cosa è [NotifyPropertyChangedInvocator] in C# quando implementa INotifyPropertyChanged?
Il primo:
public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } }
La seconda:
public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } }
In secondo quella che vedete non v'è un attributo aggiuntivo [NotifyPropertyChangedInvocator]
sul metodo OnPropertyChanged
Nel mio caso si comportano entrambi allo stesso modo, ma perché, perché e quando utilizzare questo [NotifyPropertyChangedInvocator]
, quali sono i vantaggi di questo? Ho cercato su internet ma non ho trovato nessuna buona risposta.
Is è una caratteristica C#. Resharper è solo uno strumento e non è in grado di aggiungere alcun codice aggiuntivo che C# non sia supportato. C'è anche qualcosa dietro NotifyPropertyChangedInvocator. –
Quindi dici che [CallerMemberName] è anche aggiunto da Resharper, Rimuovendo anche il codice funziona, ma CallMemberName risolve il problema di dare un nome o proprietà dove lo chiamiamo. Stesso voglio sapere su NotifyPropertyChangedInvocator controllare qui http://www.kunal-chowdhury.com/2012/07/whats-new-in-c-50-learn-about.html –
@WaqasIdrees CallMemberName è una funzionalità di C# con .net 4.5. Consente di ottenere il metodo o il nome della proprietà del chiamante per il metodo. Puoi provare che NotifyPropertyChangedInvocator è dotato di C#?dato che hai downvoted – Sajeetharan