Код: Выделить всё
public class InterfaceMappingResolver : IJsonTypeInfoResolver
{
private readonly IJsonTypeInfoResolver _default = new DefaultJsonTypeInfoResolver();
private static readonly Dictionary _map = new()
{
{ typeof(IClass), typeof(ClassImplementation) }
};
public JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
var result = _default.GetTypeInfo(type, options);
if (_map.TryGetValue(type, out var impl))
{ result.CreateObject = () => Activator.CreateInstance(impl); }
return result;
}
}
< /code>
Но с типами, зарегистрированными через DI, экземпляр создается DI и т. Д. I tried to fool the TypeInfoResolver by just returning result = _default.GetTypeInfo(impl, options)System.InvalidOperationException: 'The IJsonTypeInfoResolver returned an incompatible JsonTypeInfo instance of type 'ClassImplementation', ожидаемый тип 'Iclass'. '< /P>
< /blockquote>
Есть идеи, как это добиться с помощью записей? Или я, наверное, здесь проиграл?>
Подробнее здесь: https://stackoverflow.com/questions/797 ... on-records
Мобильная версия