1
1
using System . Collections . Concurrent ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
4
5
5
6
namespace Rubberduck . VBEditor . ComManagement
@@ -8,7 +9,7 @@ public class StrongComSafe: ComSafeBase
8
9
{
9
10
//We override the equality comparison and hash code because subclasses of SafeComWrapper<T> override the corresponding methods.
10
11
//We need to distinguish between the individual wrapper instances no matter whether they are semantically equal.
11
- private readonly ConcurrentDictionary < int , ISafeComWrapper > _comWrapperCache = new ConcurrentDictionary < int , ISafeComWrapper > ( ) ;
12
+ private readonly ConcurrentDictionary < ISafeComWrapper , byte > _comWrapperCache = new ConcurrentDictionary < ISafeComWrapper , byte > ( new ReferenceEqualityComparer ( ) ) ;
12
13
13
14
public override void Add ( ISafeComWrapper comWrapper )
14
15
{
@@ -18,8 +19,8 @@ public override void Add(ISafeComWrapper comWrapper)
18
19
Trace = GetStackTrace ( 3 , 3 ) ;
19
20
#endif
20
21
_comWrapperCache . AddOrUpdate (
21
- GetComWrapperObjectHashCode ( comWrapper ) ,
22
- value => comWrapper ,
22
+ comWrapper ,
23
+ key => 1 ,
23
24
( key , value ) =>
24
25
{
25
26
#if DEBUG
@@ -32,8 +33,7 @@ public override void Add(ISafeComWrapper comWrapper)
32
33
33
34
public override bool TryRemove ( ISafeComWrapper comWrapper )
34
35
{
35
- return ! _disposed && comWrapper != null &&
36
- _comWrapperCache . TryRemove ( GetComWrapperObjectHashCode ( comWrapper ) , out _ ) ;
36
+ return ! _disposed && comWrapper != null && _comWrapperCache . TryRemove ( comWrapper , out _ ) ;
37
37
}
38
38
39
39
private bool _disposed ;
@@ -46,7 +46,7 @@ protected override void Dispose(bool disposing)
46
46
47
47
_disposed = true ;
48
48
49
- foreach ( var comWrapper in _comWrapperCache . Values )
49
+ foreach ( var comWrapper in _comWrapperCache . Keys )
50
50
{
51
51
comWrapper . Dispose ( ) ;
52
52
}
@@ -57,7 +57,7 @@ protected override void Dispose(bool disposing)
57
57
#if DEBUG
58
58
protected override IDictionary < int , ISafeComWrapper > GetWrappers ( )
59
59
{
60
- return _comWrapperCache ;
60
+ return _comWrapperCache . Keys . ToDictionary ( GetComWrapperObjectHashCode ) ;
61
61
}
62
62
#endif
63
63
}
0 commit comments