Skip to content

Commit 1e3f29a

Browse files
committed
Implement GetCachedConversionPath
1 parent 10a799f commit 1e3f29a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/ConvertEx/TypeConverter.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class TypeConverter : ITypeConverter
1313
{
1414
private readonly List<ITypeDigester> _digesters = new();
1515
private readonly List<ITypeConverter> _converters = new();
16+
private readonly Dictionary<Tuple<Type, Type>, IList<Type>> _conversionPathCache = new();
1617

1718
public TypeConverter()
1819
{
@@ -111,10 +112,23 @@ bool TestSpecialConvert(out object convertedValue)
111112
return true;
112113
}
113114

115+
/// <summary>
116+
/// Clears the internal cache of conversion paths.
117+
/// </summary>
118+
public void ClearCache()
119+
{
120+
_conversionPathCache.Clear();
121+
}
122+
114123
private IList<Type> GetCachedConversionPath(Type sourceType, Type destType)
115124
{
116-
// TODO implement GetCachedConversionPath properly
117-
return GetConversionPath(sourceType, destType);
125+
var typeTuple = new Tuple<Type, Type>(sourceType, destType);
126+
if (_conversionPathCache.TryGetValue(typeTuple, out var path))
127+
return path;
128+
129+
path = GetConversionPath(sourceType, destType);
130+
_conversionPathCache[typeTuple] = path;
131+
return path;
118132
}
119133

120134
private IList<Type> GetConversionPath(Type sourceType, Type destType)

0 commit comments

Comments
 (0)