12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.ComponentModel;
- using System.Linq.Expressions;
- using System.Runtime.CompilerServices;
- namespace Flyinsono.Client.Test
- {
- /// <summary>
- /// Implementation of INotifyPropertyChanged
- /// </summary>
- public abstract class NotificationObject : INotifyPropertyChanged
- {
- /// <summary>
- /// Property change handler
- /// </summary>
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
- }
- protected void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
- {
- var propertyName = GetPropertyName(propertyExpression);
- OnPropertyChanged(propertyName);
- }
- protected void OnPropertyChanged(params Expression<Func<object>>[] propertyExpressions)
- {
- foreach (var expression in propertyExpressions)
- {
- OnPropertyChanged<object>(expression);
- }
- }
- private static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
- {
- if (propertyExpression == null)
- {
- throw new ArgumentNullException("propertyExpression");
- }
- var memberExpression = propertyExpression.Body as MemberExpression;
- if (memberExpression != null)
- {
- return memberExpression.Member.Name;
- }
- var unaryExpression = propertyExpression.Body as UnaryExpression;
- if (unaryExpression != null)
- {
- var memberInfo = (MemberExpression)unaryExpression.Operand;
- return memberInfo.Member.Name;
- }
- return string.Empty;
- }
- }
- }
|