内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/28397256/is-there-any-benefit-of-using-the-nameof-operator-instead-of-the-callermembernam
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那个
produces this for CallerMemberNameAttribute
:
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));
}
}
和
this for nameof
:
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Android RxJava 操作符详解系列:条件 / 布尔操作符
- C语言中点操作符(.)和箭头操作符(->)的不同之处
- JavaScript骚操作之操作符
- JS操作符拾遗
- 浅谈JavaScript位操作符
- rxjs switchMap操作符
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTML5和CSS3实例教程
Brian P.Hogan / 李杰、刘晓娜、柳靖、朱嵬 / 人民邮电出版社 / 2012-1 / 39.00元
《HTML5和CSS3实例教程》共分3部分,集中讨论了HTML5和CSS3规范及其技术的使用方法。首先是规范概述,介绍了新的结构化标签、表单域及其功能(包括自动聚焦功能和占位文本)和CSS3的新选择器。接下来是HTML对视频和音频的支持,讲述了画布上的图形绘制及CSS阴影、渐变和变换的使用方法。最后介绍使用HTML5的客户端特性(包括WebStorage、WebSQLDatabases以及离线支持......一起来看看 《HTML5和CSS3实例教程》 这本书的介绍吧!