Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi. My question is, is there a difference between this:

C#
private ScreenMenuCategory _selectedCategory;
public ScreenMenuCategory SelectedCategory
{
     get { return _selectedCategory; }
     set { _selectedCategory = value; RaisePropertyChanged(() => SelectedCategory); }
}


and this?

C#
private ScreenMenuCategory _selectedCategory;
public ScreenMenuCategory SelectedCategory
{
    get { return _selectedCategory; }
    set { _selectedCategory = value; RaisePropertyChanged(SelectedCategory); }
}


Thanks in advance.
Posted
Comments
Richard C Bishop 25-Sep-13 10:33am    
Well, obviously there is a syntax difference. Do they both compile? Do they both do the same thing?
YourAverageCoder 25-Sep-13 10:39am    
It's just a random code i made up. Obviously there are differences but i don't get them.
Richard C Bishop 25-Sep-13 10:44am    
Very well, does the top example even compile?
YourAverageCoder 25-Sep-13 10:51am    
No, it says it can't convert from some object to string[].
Richard C Bishop 25-Sep-13 10:57am    
I didn't think so. If RaisePropertyChanged() is a method, then the argument you are passing it is not valid code. What datatype does that method expect?

1 solution

An Expression<tdelegate> represents the abstract syntax tree of the lambda expression. So you just have to analyze this syntax tree to find out the property name:

C#
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
    var memberExpr = propertyExpression.Body as MemberExpression;
    if (memberExpr == null)
        throw new ArgumentException("propertyExpression should represent access to a member");
    string memberName = memberExpr.Member.Name;
    RaisePropertyChanged(memberName);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900