.NET 7 Preview 6

Par:
fredericmazue

mer, 13/07/2022 - 11:48

Microsoft a publié .NET 7 Preview 6. Ce nouvel aperçu de .NET 7 inclut notamment des améliorations des convertisseurs de type et la personnalisation des contrats JSON.

Ainsi, Il existe désormais des convertisseurs de type exposés pour les types primitifs nouvellement ajoutés DateOnly, TimeOnly, Int128, UInt128 et Half.

namespace System.ComponentModel
{
    public class DateOnlyConverter : System.ComponentModel.TypeConverter
    {
        public DateOnlyConverter() { }
    }

    public class TimeOnlyConverter : System.ComponentModel.TypeConverter
    {
        public TimeOnlyConverter() { }
    }

    public class Int128Converter : System.ComponentModel.BaseNumberConverter
    {
        public Int128Converter() { }
    }

    public class UInt128Converter : System.ComponentModel.BaseNumberConverter
    {
        public UInt128Converter() { }
    }

    public class HalfConverter : System.ComponentModel.BaseNumberConverter
    {
        public HalfConverter() { }
    }
}

Exemple d'utilisation

TypeConverter dateOnlyConverter = TypeDescriptor.GetConverter(typeof(DateOnly));
// produce DateOnly value of DateOnly(1940, 10, 9)
DateOnly? date = dateOnlyConverter.ConvertFromString("1940-10-09") as DateOnly?;

TypeConverter timeOnlyConverter = TypeDescriptor.GetConverter(typeof(TimeOnly));
// produce TimeOnly value of TimeOnly(20, 30, 50)
TimeOnly? time = timeOnlyConverter.ConvertFromString("20:30:50") as TimeOnly?;

TypeConverter halfConverter = TypeDescriptor.GetConverter(typeof(Half));
// produce Half value of -1.2
Half? half = halfConverter.ConvertFromString(((Half)(-1.2)).ToString()) as Half?;

TypeConverter Int128Converter = TypeDescriptor.GetConverter(typeof(Int128));
// produce Int128 value of Int128.MaxValue which equal 170141183460469231731687303715884105727
Int128? int128 = Int128Converter.ConvertFromString("170141183460469231731687303715884105727") as Int128?;

TypeConverter UInt128Converter = TypeDescriptor.GetConverter(typeof(UInt128));
// produce UInt128 value of UInt128.MaxValue which equal 340282366920938463463374607431768211455
UInt128? uint128 = UInt128Converter.ConvertFromString("340282366920938463463374607431768211455") as UInt128?;

Personnalisation du contrat JSON

Dans certaines situations, les développeurs qui sérialisent ou désérialisent JSON constatent qu'ils ne veulent pas ou ne peuvent pas changer de types car ceux-ci proviennent d'une bibliothèque externe ou cela polluerait considérablement le code, mais doivent apporter des modifications qui influencent la sérialisation, comme la suppression de propriétés, la modification de la façon dont les nombres être sérialisé, comment l'objet est créé, etc. Ils sont souvent obligés d'écrire des wrappers ou des convertisseurs personnalisés, ce qui est non seulement fastidieux, mais ralentit également la sérialisation.

La personnalisation du contrat JSON permet à l'utilisateur de mieux contrôler quels types sont sérialisés ou désérialisés et comment ils sont sérialisés.