using System.Collections.Concurrent;
using MessagePack;
namespace Dpz.Core.WebApi.MessagePackFormatters;
/// <summary>
/// 序列化类型缓存
/// </summary>
/// <param name="formatterResolver"></param>
public class SerializableTypesCache(IFormatterResolver formatterResolver)
{
private readonly IFormatterResolver _formatterResolver =
formatterResolver ?? throw new ArgumentNullException(nameof(formatterResolver));
private readonly ConcurrentDictionary<Type, bool> _serializableTypes = new();
/// <summary>
/// 检查类型是否可读
/// </summary>
/// <param name="type">检查类型</param>
/// <returns></returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="type" /> is <see langword="null" /></exception>
public bool CanSerialize(Type type)
{
ArgumentNullException.ThrowIfNull(type);
return _serializableTypes.GetOrAdd(
type,
x => _formatterResolver.GetFormatterDynamic(x) != null
);
}
}
AI 正在分析代码…
评论加载中...