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

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

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


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

查看所有标签

猜你喜欢:

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

游戏数据分析的艺术

游戏数据分析的艺术

于洋、余敏雄、吴娜、师胜柱 / 机械工业出版社 / 2015-7 / 79.00

《游戏数据分析的艺术》是中国游戏产业的开创性著作,具有里程碑意义,它首次系统讲解了如何对游戏行业的数据进行分析,在行业里竖起了一根标杆。作者是来自TalkingData等国内顶尖的数据分析机构和西山居这样的知名游戏公司的资深数据分析专家, 对游戏数据从不同的业务角度进行了诠释。本书详细剖析了游戏数据分析相关的指标、方法论、内容挖掘、数据挖掘、软件使用、游戏设计、运营策划、渠道推广、收入解读、用户分......一起来看看 《游戏数据分析的艺术》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具

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

HEX HSV 互换工具