You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
recyclarr/src/Common/YamlDotNet/ReadOnlyCollectionNodeTypeR...

30 lines
1.0 KiB

using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Common.YamlDotNet;
// from: https://github.com/aaubry/YamlDotNet/issues/236#issuecomment-632054372
public sealed class ReadOnlyCollectionNodeTypeResolver : INodeTypeResolver
{
public bool Resolve(NodeEvent? nodeEvent, ref Type currentType)
{
if (!currentType.IsInterface || !currentType.IsGenericType ||
!CustomGenericInterfaceImplementations.TryGetValue(currentType.GetGenericTypeDefinition(),
out var concreteType))
{
return false;
}
currentType = concreteType.MakeGenericType(currentType.GetGenericArguments());
return true;
}
private static readonly IReadOnlyDictionary<Type, Type> CustomGenericInterfaceImplementations =
new Dictionary<Type, Type>
{
{typeof(IReadOnlyCollection<>), typeof(List<>)},
{typeof(IReadOnlyList<>), typeof(List<>)},
{typeof(IReadOnlyDictionary<,>), typeof(Dictionary<,>)}
};
}