TypeMapper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Linq;
  4. namespace MediaBrowser.Server.Implementations.Reflection
  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 so that we can de-serialize properly when we don't have strong types
  13. /// </summary>
  14. private readonly ConcurrentDictionary<string, Type> _typeMap = new ConcurrentDictionary<string, Type>();
  15. /// <summary>
  16. /// Gets the type.
  17. /// </summary>
  18. /// <param name="typeName">Name of the type.</param>
  19. /// <returns>Type.</returns>
  20. /// <exception cref="System.ArgumentNullException"></exception>
  21. public Type GetType(string typeName)
  22. {
  23. if (string.IsNullOrEmpty(typeName))
  24. {
  25. throw new ArgumentNullException();
  26. }
  27. return _typeMap.GetOrAdd(typeName, LookupType);
  28. }
  29. /// <summary>
  30. /// Lookups the type.
  31. /// </summary>
  32. /// <param name="typeName">Name of the type.</param>
  33. /// <returns>Type.</returns>
  34. private Type LookupType(string typeName)
  35. {
  36. return AppDomain
  37. .CurrentDomain
  38. .GetAssemblies()
  39. .Select(a => a.GetType(typeName, false))
  40. .FirstOrDefault(t => t != null);
  41. }
  42. }
  43. }