jellyfin/Emby.Server.Implementations/Data/TypeMapper.cs

47 lines
1.4 KiB
C#
Raw Normal View History

using System;
2013-02-20 18:33:05 -07:00
using System.Collections.Concurrent;
using System.Linq;
2016-11-19 09:51:49 -07:00
namespace Emby.Server.Implementations.Data
2013-02-20 18:33:05 -07:00
{
/// <summary>
2019-11-01 10:38:54 -07:00
/// Class TypeMapper.
2013-02-20 18:33:05 -07:00
/// </summary>
public class TypeMapper
{
/// <summary>
2019-11-01 10:38:54 -07: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-20 18:33:05 -07:00
/// </summary>
private readonly ConcurrentDictionary<string, Type> _typeMap = new ConcurrentDictionary<string, Type>();
/// <summary>
/// Gets the type.
/// </summary>
/// <param name="typeName">Name of the type.</param>
/// <returns>Type.</returns>
2019-11-01 10:38:54 -07:00
/// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception>
2013-02-20 18:33:05 -07:00
public Type GetType(string typeName)
{
if (string.IsNullOrEmpty(typeName))
{
throw new ArgumentNullException(nameof(typeName));
2013-02-20 18:33:05 -07: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)
{
2019-03-07 10:10:55 -07:00
return AppDomain.CurrentDomain.GetAssemblies()
2016-11-19 09:51:49 -07:00
.Select(a => a.GetType(typeName))
.FirstOrDefault(t => t != null);
2013-02-20 18:33:05 -07:00
}
}
}