TypeMapper.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Linq;
  4. namespace Emby.Server.Implementations.Data
  5. {
  6. /// <summary>
  7. /// Class TypeMapper.
  8. /// </summary>
  9. public class TypeMapper
  10. {
  11. /// <summary>
  12. /// This holds all the types in the running assemblies
  13. /// so that we can de-serialize properly when we don't have strong types.
  14. /// </summary>
  15. private readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>();
  16. /// <summary>
  17. /// Gets the type.
  18. /// </summary>
  19. /// <param name="typeName">Name of the type.</param>
  20. /// <returns>Type.</returns>
  21. /// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception>
  22. public Type? GetType(string typeName)
  23. {
  24. ArgumentException.ThrowIfNullOrEmpty(typeName);
  25. return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies()
  26. .Select(a => a.GetType(k))
  27. .FirstOrDefault(t => t is not null));
  28. }
  29. }
  30. }