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

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

内容简介:代码日志版权声明:翻译自: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


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

查看所有标签

猜你喜欢:

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

算法神探

算法神探

[美] 杰瑞米·库比卡 / 啊哈磊、李嘉浩 / 电子工业出版社 / 2017-2 / 65

《算法神探:一部谷歌首席工程师写的CS小说》围绕程序设计典型算法,精心编织了一个扣人心弦又趣味横生的侦探缉凶故事。小说主人公运用高超的搜索技巧和精深的算法知识,最终识破阴谋、缉拿元凶。其间,用二分搜索搜查走私船、用搜索树跟踪间谍、用深度优先搜索逃离监狱、用优先队列开锁及用最佳优先搜索追寻线索等跌宕起伏又富含算法精要的情节,让读者在愉悦的沉浸式体验中快速提升境界,加深对程序世界的理解。《算法神探:一......一起来看看 《算法神探》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器