Skip to content

Commit

Permalink
Update SlackTypeResolver.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
omer-koren committed Dec 20, 2022
1 parent 36395ba commit 4b603e1
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions SlackNet/Serialization/SlackTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -14,13 +13,27 @@ public interface ISlackTypeResolver
class SlackTypeResolver : ISlackTypeResolver
{
private readonly Assembly[] _assemblies;
private readonly ConcurrentDictionary<Type, Dictionary<string, Type>> _typeLookups = new();

private Dictionary<Type, Dictionary<string, Type>> _typeLookups = new();
private object _lock = new object();
public SlackTypeResolver(params Assembly[] assemblies) => _assemblies = assemblies;

public Type FindType(Type baseType, string slackType)
{
return _typeLookups.GetOrAdd(baseType, bt => CreateLookup(bt)).TryGetValue(slackType, out var type)
Dictionary<string, Type> typeLookup;
if (!_typeLookups.TryGetValue(baseType, out typeLookup))
{
lock (_lock)
{
if (!_typeLookups.TryGetValue(baseType, out typeLookup))
{
var copy = _typeLookups.ToDictionary(x => x.Key, x => x.Value);
typeLookup = copy[baseType] = CreateLookup(baseType);
_typeLookups = copy;
}
}
}

return typeLookup.TryGetValue(slackType, out var type)
? type
: baseType;

Expand Down

0 comments on commit 4b603e1

Please sign in to comment.