Ejemplo n.º 1
0
    public PolymorphicConverter(TypeDiscrimnatorAttribute typeDiscriminator, string discriminatedField) : base()
    {
        _discriminatorField = typeDiscriminator.FieldName;
        _typeProvider       = (ITypeProvider)(typeDiscriminator.ConverterType.GetConstructor(new Type[] { })?.Invoke(null) ?? throw new JsonException());
        _discriminatedField = discriminatedField;
        _constructorInfo    = typeof(T).GetConstructors().FirstOrDefault() ?? throw new JsonException("No Constructor found");
        _parameters         = _constructorInfo.GetParameters()?.ToDictionary(x => x.Name ?? "") ?? throw new JsonException();
        _constructor        = EntityConverter.BuildConstructerFrom(_constructorInfo);
        _discriminatorType  = _parameters[_discriminatorField].ParameterType;
        _options            = new ConditionalWeakTable <JsonSerializerOptions, JsonSerializerOptions>();

        _renamedViaJsonPropery =
            typeof(T).GetProperties()
            .Select(x => (x.Name, x.GetCustomAttribute <JsonPropertyNameAttribute>()?.Name))
            .Where(x => x.Item2 != null)
            .ToDictionary(x => x.Item1, x => x.Item2 !) ?? new Dictionary <string, string>();
    }