-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChangeNotifierBase.cs
52 lines (46 loc) · 1.63 KB
/
ChangeNotifierBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Diagnostics;
using System.Reflection;
namespace Thingie.WPF
{
[Serializable]
public class ChangeNotifierBase : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged()
{
string caller = new StackFrame(1, false).GetMethod().Name;
if (caller.StartsWith("set_"))
OnPropertyChanged(caller.Substring(4));
else
throw new InvalidOperationException("Can only be called from a setter!");
}
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propFunc)
{
string propName = ((propFunc.Body as MemberExpression).Member as PropertyInfo).Name;
OnPropertyChanged(propName);
}
protected virtual void OnPropertyChanged(string propertyName)
{
VerifyPropertyName(propertyName);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "Invalid property name: " + propertyName;
Debug.Fail(msg);
}
}
#endregion
}
}