2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Linq;
|
|
|
|
|
2016-11-19 16:51:49 +00:00
|
|
|
namespace Emby.Server.Implementations.Data
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 17:38:54 +00:00
|
|
|
/// Class TypeMapper.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
|
|
|
public class TypeMapper
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 17:38:54 +00:00
|
|
|
/// This holds all the types in the running assemblies
|
|
|
|
/// so that we can de-serialize properly when we don't have strong types.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2021-05-20 19:28:18 +00:00
|
|
|
private readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="typeName">Name of the type.</param>
|
|
|
|
/// <returns>Type.</returns>
|
2019-11-01 17:38:54 +00:00
|
|
|
/// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception>
|
2021-05-20 19:28:18 +00:00
|
|
|
public Type? GetType(string typeName)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(typeName))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(typeName));
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
.Select(a => a.GetType(k))
|
|
|
|
.FirstOrDefault(t => t != null));
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|