Skip to content

Commit 282f99a

Browse files
committed
added invalidation mechanism
1 parent 7d0b34d commit 282f99a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Rubberduck.Parsing/ComReflection/TypeLibReflection/CachedTypeService.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Linq;
4+
using System.Runtime.InteropServices;
45
using System.Runtime.InteropServices.ComTypes;
56

67
namespace Rubberduck.Parsing.ComReflection.TypeLibReflection
@@ -15,14 +16,15 @@ namespace Rubberduck.Parsing.ComReflection.TypeLibReflection
1516
/// returned for each invocation, even for the same ProgID or ITypeInfo. That will cause problems later such
1617
/// as being unable to cast an instance from one type to another, even though they are based on exactly the
1718
/// same ProgID/ITypeInfo/etc.. In those cases, the <see cref="Type.IsEquivalentTo"/> incorrectly returns
18-
/// false. Thus, those methods should be wrapped in the <see cref="TryCacheType"/> methods to ensure that
19+
/// false. Thus, those methods should be wrapped in the <c>TryCachedType</c> methods to ensure that
1920
/// the repeated invocation will continue to return exactly same <see cref="Type"/>.
2021
///
2122
/// For details on the issue with the <see cref="Type.IsEquivalentTo"/>, refer to:
2223
/// https://developercommunity.visualstudio.com/content/problem/422208/typeisequivalent-does-not-behave-according-to-the.html
2324
/// </remarks>
2425
public interface ICachedTypeService
2526
{
27+
bool TryInvalidate(string project, string progId);
2628
bool TryGetCachedType(string progId, out Type type);
2729
bool TryGetCachedType(string project, string progId, out Type type);
2830
bool TryGetCachedType(ITypeInfo typeInfo, out Type type);
@@ -160,5 +162,15 @@ private static bool TryGetValue(string project, string progId, out Type type)
160162
type = null;
161163
return false;
162164
}
165+
166+
public bool TryInvalidate(string project, string progId)
167+
{
168+
if (TypeCaches.TryGetValue(project?.ToLowerInvariant() ?? string.Empty, out var cache))
169+
{
170+
return cache.Remove(progId);
171+
}
172+
173+
return false;
174+
}
163175
}
164176
}

Rubberduck.Parsing/ComReflection/TypeLibReflection/LibraryTypeCache.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal interface ILibraryTypeCache
99
bool TryGetType(string progId, out Type type);
1010
bool AddType(string progId, Type type);
1111
Type GetOrAdd(string progId, Type type);
12+
bool Remove(string progId);
1213
}
1314

1415
internal sealed class LibraryTypeCache : ILibraryTypeCache
@@ -43,5 +44,10 @@ public Type GetOrAdd(string progId, Type type)
4344
{
4445
return _cache.GetOrAdd(progId.ToLowerInvariant(), s => type);
4546
}
47+
48+
public bool Remove(string progId)
49+
{
50+
return _cache.TryRemove(progId, out _);
51+
}
4652
}
4753
}

0 commit comments

Comments
 (0)