使用 MVVM,我总是看到这两种属性方法:
private int myProperty;
public int MyProperty
{
get { return myProperty; }
set
{
myProperty = value;
NotifyPropertyChanged("MyProperty");
}
}
和
private int myProperty;
public int MyProperty
{
get { return myProperty; }
set
{
myProperty = value;
NotifyPropertyChanged(m => m.MyProperty);
}
}
第一个对 NotifyPropertyChanged 使用硬编码字符串,第二个对 NotifyPropertyChanged 使用 lambda 表达式。我不想发起辩论来询问什么是更好的解决方案,但我想了解这两种解决方案之间的区别。使用其中一种会有什么后果?
如果我错了,请纠正我,但 lambda 表达式解决方案应该使用更多内存并且应该比硬编码字符串慢,因为基类的 NotifyPropertyChanged 方法使用委托和反射。但是硬编码的字符串解决方案可能会产生愚蠢的错误,因为它是一个字符串,没有什么可以告诉我我正确地写了它。