@@ -13,6 +13,14 @@ namespace BB84.Notifications;
13
13
/// </summary>
14
14
public abstract class NotifiableObject : INotifiableObject
15
15
{
16
+ private static readonly Dictionary < string , string [ ] > PropertyChangedAttributeCache = [ ] ;
17
+ private static readonly Dictionary < string , string [ ] > PropertyChangingAttributeCache = [ ] ;
18
+
19
+ /// <summary>
20
+ /// Initializes a new instance of the <see cref="NotifiableObject"/> class.
21
+ /// </summary>
22
+ protected NotifiableObject ( ) => FillCaches ( ) ;
23
+
16
24
/// <inheritdoc/>
17
25
public event PropertyChangedEventHandler ? PropertyChanged ;
18
26
@@ -79,16 +87,12 @@ protected void RaisePropertyChanging<T>(string propertyName, T value)
79
87
/// <param name="propertyName">The name of the calling property.</param>
80
88
private void RaiseChangedAttribute ( string propertyName )
81
89
{
82
- PropertyInfo ? propertyInfo = GetType ( ) . GetProperty ( propertyName ) ;
90
+ bool success = PropertyChangedAttributeCache . TryGetValue ( propertyName , out string [ ] ? propertiesToNotify ) ;
83
91
84
- if ( propertyInfo is not null )
92
+ if ( success && propertiesToNotify is not null )
85
93
{
86
- NotifyChangedAttribute ? attribute =
87
- propertyInfo . GetCustomAttribute ( typeof ( NotifyChangedAttribute ) , false ) as NotifyChangedAttribute ;
88
-
89
- if ( attribute is not null )
90
- foreach ( string property in attribute . Properties )
91
- RaisePropertyChanged ( property ) ;
94
+ foreach ( string propertyToNotify in propertiesToNotify )
95
+ RaisePropertyChanged ( propertyToNotify ) ;
92
96
}
93
97
}
94
98
@@ -99,16 +103,37 @@ private void RaiseChangedAttribute(string propertyName)
99
103
/// <param name="propertyName">The name of the calling property.</param>
100
104
private void RaiseChangingAttribute ( string propertyName )
101
105
{
102
- PropertyInfo ? propertyInfo = GetType ( ) . GetProperty ( propertyName ) ;
106
+ bool success = PropertyChangingAttributeCache . TryGetValue ( propertyName , out string [ ] ? propertiesToNotify ) ;
103
107
104
- if ( propertyInfo is not null )
108
+ if ( success && propertiesToNotify is not null )
105
109
{
106
- NotifyChangingAttribute ? attribute =
110
+ foreach ( string propertyToNotify in propertiesToNotify )
111
+ RaisePropertyChanging ( propertyToNotify ) ;
112
+ }
113
+ }
114
+
115
+ /// <summary>
116
+ /// The method fills the <see cref="PropertyChangedAttributeCache"/> and the <see cref="PropertyChangingAttributeCache"/>
117
+ /// when the class is instantiated so that it only has to do this once. This means that subsequent calls to
118
+ /// <see cref="RaiseChangedAttribute"/> and <see cref="RaiseChangingAttribute"/> no longer have to do this.
119
+ /// </summary>
120
+ private void FillCaches ( )
121
+ {
122
+ PropertyInfo [ ] propertiesInfos = GetType ( ) . GetProperties ( ) ;
123
+
124
+ foreach ( PropertyInfo propertyInfo in propertiesInfos )
125
+ {
126
+ NotifyChangedAttribute ? changedAttribute =
127
+ propertyInfo . GetCustomAttribute ( typeof ( NotifyChangedAttribute ) , false ) as NotifyChangedAttribute ;
128
+
129
+ if ( changedAttribute is not null )
130
+ PropertyChangedAttributeCache . Add ( propertyInfo . Name , changedAttribute . Properties ) ;
131
+
132
+ NotifyChangingAttribute ? changingAttribute =
107
133
propertyInfo . GetCustomAttribute ( typeof ( NotifyChangingAttribute ) , false ) as NotifyChangingAttribute ;
108
134
109
- if ( attribute is not null )
110
- foreach ( string property in attribute . Properties )
111
- RaisePropertyChanging ( property ) ;
135
+ if ( changingAttribute is not null )
136
+ PropertyChangingAttributeCache . Add ( propertyInfo . Name , changingAttribute . Properties ) ;
112
137
}
113
138
}
114
139
}
0 commit comments