2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2016-11-19 16:51:49 +00:00
|
|
|
|
using MediaBrowser.Model.Reflection;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
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>
|
|
|
|
|
/// Class TypeMapper
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class TypeMapper
|
|
|
|
|
{
|
2016-11-19 16:51:49 +00:00
|
|
|
|
private readonly IAssemblyInfo _assemblyInfo;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This holds all the types in the running assemblies so that we can de-serialize properly when we don't have strong types
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, Type> _typeMap = new ConcurrentDictionary<string, Type>();
|
|
|
|
|
|
2016-11-19 16:51:49 +00:00
|
|
|
|
public TypeMapper(IAssemblyInfo assemblyInfo)
|
|
|
|
|
{
|
|
|
|
|
_assemblyInfo = assemblyInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="typeName">Name of the type.</param>
|
|
|
|
|
/// <returns>Type.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException"></exception>
|
|
|
|
|
public Type GetType(string typeName)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _typeMap.GetOrAdd(typeName, LookupType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lookups the type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="typeName">Name of the type.</param>
|
|
|
|
|
/// <returns>Type.</returns>
|
|
|
|
|
private Type LookupType(string typeName)
|
|
|
|
|
{
|
2016-11-19 16:51:49 +00:00
|
|
|
|
return _assemblyInfo
|
|
|
|
|
.GetCurrentAssemblies()
|
|
|
|
|
.Select(a => a.GetType(typeName))
|
|
|
|
|
.FirstOrDefault(t => t != null);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|