随着WPF4.5.3的到来,.NET开发人员现在有三种(或更多)方法来通知INotifyPropertyChanged
Interface属性更改。基本上,我的问题是,从WPF4.5开始引入的两种方法中,哪一种是更有效的通知属性更改的方式,以及在from.NET中使用这两种方式是否有任何好处?
背景
对于那些不太熟悉这个主题的人,这里有三种主要的方法。第一种是最初的、更容易出错的方法,简单地传递一个字符串:
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
操作符是否真的有什么不同。
发布于 2015-02-09 02:03:53
关于效率:直接使用字符串,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));
}
}
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));
}
}
因为在运行时所有的选项都是一个简单的string
,所以WPF上下文没有问题。
关于便利性:CallerMemberNameAttribute
要求您有一个可选参数,而nameof
没有,但nameof
要求您指定属性,而CallerMemberNameAttribute
没有。
我预测nameof
将变得如此流行,以至于使用它会简单得多。
发布于 2015-02-09 03:31:03
CallerMemberNameAttribute
只能用于被调用的函数,以获取调用方函数的名称。
nameof
操作符远远超出了这一点。它可以在任何地方使用。
如果您只想在WPF数据绑定的范围内对其进行推理,请使用以下示例:
public string FullName
{
get
{
return string.Format(
"{0} {1}",
this.firstName,
this.lastName);
}
}
public string FirstName
{
get
{
return this.firstName;
}
set
{
if (value != this.firstName)
{
this.firstName = value;
NotifyPropertyChanged(nameof(FirstName));
NotifyPropertyChanged(nameof(FullName));
}
}
}
public string LasttName
{
get
{
return this.lastName;
}
set
{
if (value != this.lastName)
{
this.lastName = value;
NotifyPropertyChanged(nameof(LasttName));
NotifyPropertyChanged(nameof(FullName));
}
}
}
https://stackoverflow.com/questions/28397256
复制相似问题