c# – 使用nameof操作符而不是CallerMemberNameAttribute来通知.NET 4.5.3中的属性更改有什么好处吗?

栏目: ASP.NET · 发布时间: 5年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/28397256/is-there-any-benefit-of-using-the-nameof-operator-instead-of-the-callermembernam
随着.NET 4.5.3的出现,WPF开发人员现在有三种(或多种)通知 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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Mathematica演示项目笔记

Mathematica演示项目笔记

吴飞 / 清华大学出版社 / 2010-7 / 39.00元

Mathematica是由美国科学家斯蒂芬·沃尔夫勒姆(Stephen Wolfram)领导的Wolfram Research Inc.研究公司所开发的一体化计算引擎。《Mathematica演示项目笔记》结合Mathematica演示项目以及第6版和第7版的最新功能(动态交互性、即时三维图形、数值模拟和仿真、高效实时计算、集成语言系统、多核并行计算和数字图像处理等),讲解了符号计算、程序设计、算......一起来看看 《Mathematica演示项目笔记》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具