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


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

查看所有标签

猜你喜欢:

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

失业的程序员

失业的程序员

沈逸 / 2014-5-1 / 39.00元

这是一个程序员从失业到自行创业的奋斗历程,虽然囧事连连、过程曲折,却充满了趣味。本书以作者的真实创业经历为主线,文字幽默诙谐,情节生动真实,包括了招聘、团队管理和用户公关,以及技术架构设计、核心代码编写、商务谈判、项目运作等场景经验。 从初期的创业伙伴、领路人,到商业竞争对手,各种复杂的关系在各个关键时刻却都发生了意想不到的逆转。在历经千辛万苦,眼看快要成功时,主人公却几乎再次失业。 ......一起来看看 《失业的程序员》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具