gpt4 book ai didi

linq - Linq表达式帮助-INotifyPropertyChanged

转载 作者:行者123 更新时间:2023-12-03 10:53:01 26 4
gpt4 key购买 nike

我正在阅读最新的Prism 4发行版的源代码,并且对解决此问题感兴趣。 ViewModels有一个基类,它实现INotifyPropertyChanged和INotifyDataErrorInfo并提供一些重构友好的更改通知。

protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
{
var propertyName = ExtractPropertyName(propertyExpresssion);
this.RaisePropertyChanged(propertyName);
}



private string ExtractPropertyName<T>(Expression<Func<T>> propertyExpresssion)
{
if (propertyExpresssion == null)
{
throw new ArgumentNullException("propertyExpression");
}

var memberExpression = propertyExpresssion.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("The expression is not a member access expression.", "propertyExpression");
}

var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException("The member access expression does not access property.","propertyExpression");
}

if (!property.DeclaringType.IsAssignableFrom(this.GetType()))
{
throw new ArgumentException("The referenced property belongs to a different type.", "propertyExpression");
}

var getMethod = property.GetGetMethod(true);
if (getMethod == null)
{
// this shouldn't happen - the expression would reject the property before reaching this far
throw new ArgumentException("The referenced property does not have a get method.", "propertyExpression");
}

if (getMethod.IsStatic)
{
throw new ArgumentException("The referenced property is a static property.", "propertyExpression");
}

return memberExpression.Member.Name;
}

并举例说明它的用法
private void RetrieveNewQuestionnaire()
{
this.Questions.Clear();

var template = this.questionnaireService.GetQuestionnaireTemplate();
this.questionnaire = new Questionnaire(template);

foreach (var question in this.questionnaire.Questions)
{
this.Questions.Add(this.CreateQuestionViewModel(question));
}

this.RaisePropertyChanged(() => this.Name);
this.RaisePropertyChanged(() => this.UnansweredQuestions);
this.RaisePropertyChanged(() => this.TotalQuestions);
this.RaisePropertyChanged(() => this.CanSubmit);
}

我的问题是这个。将属性名称的数组传递给重载的方法(RaisePropertyChanged),并将最后这段代码从4行压缩为1,该怎么办?

谢谢,
史提芬

最佳答案

您是否考虑过更改:

protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
{
var propertyName = ExtractPropertyName(propertyExpresssion);
this.RaisePropertyChanged(propertyName);
}

到 :
protected void RaisePropertyChanged<T>(params Expression<Func<T>>[] propertyExpresssion)
{
foreach (var propertyName in
propertyExpresssion.Select(ExtractPropertyName))
{
this.RaisePropertyChanged(propertyName);
}
}

用法是:
private void RetrieveNewQuestionnaire()
{
//yada yada yada
this.RaisePropertyChanged(() => this.Name,
() => this.UnansweredQuestions,
() => this.TotalQuestions);
}

也许有人认为这是一种不好的做法,但至少可以做到这一点。
祝你好运。

关于linq - Linq表达式帮助-INotifyPropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3030985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com