Help with Linq Expression - INotifyPropertyChanged

Posted by Stephen Patten on Stack Overflow See other posts from Stack Overflow or by Stephen Patten
Published on 2010-06-13T03:42:21Z Indexed on 2010/06/13 3:52 UTC
Read the original article Hit count: 411

Filed under:
|
|

Hello,

I'm reading the source code from the latest Prism 4 drop and am interested in solving this problem. There is a base class for the ViewModels that implements INotifyPropertyChanged and INotifyDataErrorInfo and provides some refactoring friendly change notification.

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;
    }

and as an example of it's usage

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);
}

My question is this. What would it take to pass an array of the property names to an overloaded method (RaisePropertyChanged) and condense this last bit of code from 4 lines to 1?

Thank you, Stephen

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about databinding