c# – 使用nameof操作符而不是CallerMemberNameAttribute來通知.NET 4.5.3中的屬性更改有什麼好處嗎?
INotifyPropertyChanged
Interface
屬性更改的方法.基本上,我的問題是從.NET 4.5開始引入的兩種方法中的哪一種是通知屬性更改的更有效的方法,以及在WPF中使用時是否有任何好處?
背景
對於那些不太熟悉這個問題的人來說,這裡主要有三種方法.第一個是原來的,更容易出錯的方法,只是傳遞一個字串:
public string TestValue { get { return testValue; } set { testValue = value; NotifyPropertyChanged("TestValue"); } } protected virtual void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
第二種方法在.NET 4.5中引入;
CallerMemberNameAttribute
:
public string TestValue { get { return testValue; } set { testValue = value; NotifyPropertyChanged(); } } protected virtual void NotifyPropertyChanged([CallerMemberName]string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
作為.NET 4.5.3的一部分,C#6.0中引入了第三個也是最近的方法(或即將推出)
nameof
Operator
:
public string TestValue { get { return testValue; } set { testValue = value; NotifyPropertyChanged(nameof(TestValue)); } } protected virtual void NotifyPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
我自己的假設是,原來的,更容易出錯的簡單傳遞字串的方法將是最有效的,因為我只能想象其他兩種方法使用某種形式的反射.然而,我真的很想找出其他兩種方法中哪些方法更有效率,以及WPF上下文中使用CallerMemberNameAttribute屬性和nameof運算子之間是否會有任何區別.
關於效率:直接使用字串,CallerMemberNameAttribute,nameof都完全相同,因為編譯器在編譯時注入該字串.沒有反思.
我們可以看到使用TryRoslyn那個OAoBuYAMaoQOasABGHdTDUcAhiBAwyMAN7Y+favLDKTaGCie80KAGomOwKg49jCOzq6oMACiygzIABImEKxy8gA8AAryFNQKyAhkABZJUKisMXEAgvJQIAB8MFk5eQXF0GXaegZq7siesAAqmsh+AajYtiGlyDYw8qjIwPIQMN4j/oGYMAC+dvYgC7Nro4EwALwwLmNbAHIUyGAAZghNufL5RSVlABQAlFu7XbYRz3VBqNCsS5gd7AfyXChgSF3B7PV4tT7tVjfADaZH8qSYGF0ChuJgkAF0PPhQtk3vlSRJzjAAETM34TPZ8J4wb5o96tL6QgCEFwgwA4HHZIUmIXsfI+bVKWOQhTAIAANDB6AB3Rq09GKsoVRjVWrfEF0hAM1C/f6cnbYXYhR1AA==" rel="nofollow,noindex" target="_blank">
produces this forCallerMemberNameAttribute
:
public string TestValue { get { return this.testValue; } set { this.testValue = value; this.NotifyPropertyChanged("TestValue"); } } protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
和QQJhIUR7fIa+DaoACU4K2u122Ec91QajQrEuYHewH8lwoYBRdwez1eLU+7VY3w8+FC2Te+Ru/whEz2fEJ71aX1YAH4SABJCDKCgAa1Q32QhTAIAANDB6AB3RpUoltUrlWKMaq1b7w6kIWkSSE4XYhbDbIAAA==" rel="nofollow,noindex" target="_blank">
this fornameof
:
public string TestValue { get { return this.testValue; } set { this.testValue = value; this.NotifyPropertyChanged("TestValue"); } } protected virtual void NotifyPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
因為在執行時,所有的選項都只是一個字串,WPF上下文沒有問題.
關於方便:CallerMemberNameAttribute要求您有一個可選引數,而nameof不是,但是nameof需要您在CallerMemberNameAttribute不指定時指定該屬性.
我預測這個名字會變得如此受歡迎,以至於使用它更簡單.
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/28397256/is-there-any-benefit-of-using-the-nameof-operator-instead-of-the-callermembernam