nameof
使用overexpressions
提取属性名称是个好主意吗?
//method with expression
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression, bool isValid, [param: Localizable(true)] string validationError)
{
string propertyName = PropertySupport.ExtractPropertyName(propertyExpression);
RaisePropertyChanged(propertyName, isValid, validationError);
}
//the same logic without expression
protected void RaisePropertyChanged(string propertyName, [param: Localizable(true)] string validationError)
{
RaisePropertyChanged(propertyName, validationError == null, validationError);
}
但是叫法不一样
public string Url
{
set
{
//with nameof
RaisePropertyChanged(nameof(Url), this.GetValidationError());
//with expression
RaisePropertyChanged(() => Url, this.GetValidationError());
}
}
您知道每种方法有哪些优点和缺点?还是只有执行速度很重要?我的意思是编译nameof
后将被替换。const