Fixes for types sometimes choosing type they inherit from instead of themselves

This commit is contained in:
2019-01-22 18:32:29 +01:00
parent 8bd5f7d0af
commit 4062d2f140
6 changed files with 51 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes;
@@ -17,12 +18,21 @@ namespace Upsilon.BoundTypes
dic.TryAdd("function", new BoundTypeDefinition(Type.Function, new System.Type[0]));
}
private static readonly ConcurrentDictionary<System.Type, string> TypeLookup =
new ConcurrentDictionary<System.Type, string>();
private static readonly ConcurrentDictionary<string, BoundTypeDefinition> TypeDefinitions = Reset();
public static ConcurrentDictionary<string, BoundTypeDefinition> Reset()
{
var dic = new ConcurrentDictionary<string, BoundTypeDefinition>();
AddDefaults(dic);
foreach (var boundTypeDefinition in dic)
{
foreach (var valueValidInternalType in boundTypeDefinition.Value.ValidInternalTypes)
{
TypeLookup.TryAdd(valueValidInternalType, boundTypeDefinition.Key);
}
}
return dic;
}
@@ -43,14 +53,28 @@ namespace Upsilon.BoundTypes
public static string GetTypeName(System.Type type)
{
return TypeDefinitions.FirstOrDefault(x =>
x.Value.ValidInternalTypes.Any(validType => validType.IsAssignableFrom(type))).Key;
if (TypeLookup.TryGetValue(type, out var bDefKey))
{
return bDefKey;
}
var bDef = TypeDefinitions.FirstOrDefault(x =>
x.Value.ValidInternalTypes.Any(validType => validType.IsAssignableFrom(type)));
if (!bDef.Equals(default(KeyValuePair<string, BoundTypeDefinition>)))
{
TypeLookup.TryAdd(type, bDef.Key);
}
return null;
}
public static void LoadUserDataTypeDefinition(UserDataBoundTypeDefinition def)
{
var key = def.Name.ToLowerInvariant();
TypeDefinitions.AddOrUpdate(key, def, (s, definition) => definition);
foreach (var valueValidInternalType in def.ValidInternalTypes)
{
TypeLookup.TryAdd(valueValidInternalType, key);
}
}
}
}