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

34 lines
1.1 KiB
C#
Raw Normal View History

using System;
2013-02-21 02:33:05 +01:00
using System.Collections.Concurrent;
using System.Linq;
2016-11-19 17:51:49 +01:00
namespace Emby.Server.Implementations.Data
2013-02-21 02:33:05 +01:00
{
/// <summary>
2019-11-01 18:38:54 +01:00
/// Class TypeMapper.
2013-02-21 02:33:05 +01:00
/// </summary>
public class TypeMapper
{
/// <summary>
2019-11-01 18:38:54 +01: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 02:33:05 +01:00
/// </summary>
private readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>();
2013-02-21 02:33:05 +01:00
/// <summary>
/// Gets the type.
/// </summary>
/// <param name="typeName">Name of the type.</param>
/// <returns>Type.</returns>
2019-11-01 18:38:54 +01:00
/// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception>
public Type? GetType(string typeName)
2013-02-21 02:33:05 +01:00
{
ArgumentException.ThrowIfNullOrEmpty(typeName);
2013-02-21 02:33:05 +01:00
2021-05-24 00:30:41 +02:00
return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies()
.Select(a => a.GetType(k))
2022-12-05 15:01:13 +01:00
.FirstOrDefault(t => t is not null));
2013-02-21 02:33:05 +01:00
}
}
}