@@ -27,6 +27,9 @@ public HierarchyDefCollectionChangedEventArgs(HierarchyDef item)
27
27
/// </summary>
28
28
public class HierarchyDefCollection : CollectionBaseSlim < HierarchyDef >
29
29
{
30
+ private readonly Dictionary < Type , HierarchyDef > hierarchyDefByTypeCache = new ( ) ;
31
+ private bool invalidateCache ;
32
+
30
33
public event EventHandler < HierarchyDefCollectionChangedEventArgs > Added ;
31
34
public event EventHandler < HierarchyDefCollectionChangedEventArgs > Removed ;
32
35
@@ -43,10 +46,7 @@ public override bool Contains(HierarchyDef item)
43
46
/// <param name="key">The key of the value to get.</param>
44
47
/// <returns>The value associated with the specified <paramref name="key"/> or <see langword="null"/>
45
48
/// if item was not found.</returns>
46
- public HierarchyDef TryGetValue ( TypeDef key )
47
- {
48
- return TryGetValue ( key . UnderlyingType ) ;
49
- }
49
+ public HierarchyDef TryGetValue ( TypeDef key ) => TryGetValue ( key . UnderlyingType ) ;
50
50
51
51
/// <summary>
52
52
/// Gets the value associated with the specified key.
@@ -56,9 +56,11 @@ public HierarchyDef TryGetValue(TypeDef key)
56
56
/// if item was not found.</returns>
57
57
public HierarchyDef TryGetValue ( Type key )
58
58
{
59
- foreach ( HierarchyDef item in this )
60
- if ( item . Root . UnderlyingType == key )
59
+ foreach ( var item in this ) {
60
+ if ( item . Root . UnderlyingType == key ) {
61
61
return item ;
62
+ }
63
+ }
62
64
return null ;
63
65
}
64
66
@@ -67,13 +69,14 @@ public HierarchyDef TryGetValue(Type key)
67
69
/// </summary>
68
70
/// <exception cref="ArgumentException"> when item was not found.</exception>
69
71
public HierarchyDef this [ Type key ] =>
70
- TryGetValue ( key ) ?? throw new ArgumentException ( String . Format ( Strings . ExItemByKeyXWasNotFound , key ) , " key" ) ;
72
+ TryGetValue ( key ) ?? throw new ArgumentException ( string . Format ( Strings . ExItemByKeyXWasNotFound , key ) , nameof ( key ) ) ;
71
73
72
74
/// <inheritdoc/>
73
75
public override void Add ( HierarchyDef item )
74
76
{
75
77
base . Add ( item ) ;
76
78
Added ? . Invoke ( this , new HierarchyDefCollectionChangedEventArgs ( item ) ) ;
79
+ invalidateCache = true ;
77
80
}
78
81
79
82
/// <inheritdoc/>
@@ -89,6 +92,7 @@ public override bool Remove(HierarchyDef item)
89
92
{
90
93
if ( base . Remove ( item ) ) {
91
94
Removed ? . Invoke ( this , new HierarchyDefCollectionChangedEventArgs ( item ) ) ;
95
+ invalidateCache = true ;
92
96
return true ;
93
97
}
94
98
return false ;
@@ -100,11 +104,10 @@ public override void Clear()
100
104
foreach ( var item in this ) {
101
105
Removed ? . Invoke ( this , new HierarchyDefCollectionChangedEventArgs ( item ) ) ;
102
106
}
107
+ invalidateCache = true ;
103
108
base . Clear ( ) ;
104
109
}
105
110
106
- private readonly Dictionary < Type , HierarchyDef > hierarchyDefByTypeCache = new ( ) ;
107
-
108
111
/// <summary>
109
112
/// Finds the hierarchy.
110
113
/// </summary>
@@ -115,24 +118,23 @@ public HierarchyDef Find(TypeDef item)
115
118
ArgumentValidator . EnsureArgumentNotNull ( item , "item" ) ;
116
119
var itemUnderlyingType = item . UnderlyingType ;
117
120
118
- if ( ! hierarchyDefByTypeCache . TryGetValue ( itemUnderlyingType , out var hierarchyDef ) ) {
119
- hierarchyDef = this . FirstOrDefault ( hierarchy => hierarchy . Root . UnderlyingType . IsAssignableFrom ( itemUnderlyingType ) ) ;
120
- hierarchyDefByTypeCache . Add ( itemUnderlyingType , hierarchyDef ) ;
121
+ HierarchyDef hierarchyDef ;
122
+ if ( invalidateCache ) {
123
+ hierarchyDefByTypeCache . Clear ( ) ;
124
+ invalidateCache = false ;
125
+
126
+ FindAndCache ( itemUnderlyingType , out hierarchyDef ) ;
127
+ }
128
+ else if ( ! hierarchyDefByTypeCache . TryGetValue ( itemUnderlyingType , out hierarchyDef ) ) {
129
+ FindAndCache ( itemUnderlyingType , out hierarchyDef ) ;
121
130
}
122
131
return hierarchyDef ;
123
- }
124
-
125
- private void ClearCache ( object _ , HierarchyDefCollectionChangedEventArgs __ )
126
- {
127
- hierarchyDefByTypeCache . Clear ( ) ;
128
- }
129
132
130
- public HierarchyDefCollection ( )
131
- {
132
- Added += ClearCache ;
133
- Removed += ClearCache ;
133
+ void FindAndCache ( Type underlyingType , out HierarchyDef hierarchyDef1 )
134
+ {
135
+ hierarchyDef1 = this . FirstOrDefault ( hierarchy => hierarchy . Root . UnderlyingType . IsAssignableFrom ( underlyingType ) ) ;
136
+ hierarchyDefByTypeCache . Add ( itemUnderlyingType , hierarchyDef1 ) ;
137
+ }
134
138
}
135
-
136
- private void HierarchyDefCollection_Removed ( object sender , HierarchyDefCollectionChangedEventArgs e ) => throw new NotImplementedException ( ) ;
137
139
}
138
140
}
0 commit comments