2014-04-22 10 views
29

Vedo due tipi di implementazione di INotifyPropertyChangedCosa è [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.

risposta

4

NotifyPropertyChangedInvocator è una funzionalità Resharper.

Si può semplicemente rimuovere dal codice in modo per farlo funzionare

domanda simile è stato chiesto qui:

does anyone know how to get the [NotifyPropertyChangedInvocator]

+0

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. –

+0

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 –

+0

@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

57

Si tratta di un attributo ReSharper dal loro Annotations - progettato per fornire avvertimento quindi il codice sia sospetto :)
Considerate questo:

public class Foo : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void NotifyChanged(string propertyName) { ... } 

    private string _name; 
    public string Name { 
     get { return _name; } 
     set { 
      _name = value; 
      NotifyChanged("LastName");//<-- warning here 
      } 
    } 
    } 

Con il [NotifyPropertyChangedInvocator] attributo sul metodo NotifyChangedIl resharper fornisce un avviso, che si sta invocando il metodo con un valore (presumibilmente) errato.

Perché ReSharper ora sa che metodo deve essere chiamato a fare notifica di modifica, che vi aiuterà a convertire normali proprietà in proprietà con notifica di modifica: enter image description here
convertendolo in questo:

public string Name 
{ 
    get { return _name; } 
    set 
    { 
    if (value == _name) return; 
    _name = value; 
    NotifyChange("Name"); 
    } 
} 



Questo esempio proviene dalla documentazione sull'attributo [NotifyPropertyChangedInvocator] trovato qui:
enter image description here

+9

Più 1 per la spiegazione e non solo la risposta con un collegamento. Questo dovrebbe essere selezionato come risposta. – user3791372

Problemi correlati