.NET 7 Preview 6 发布

.NET 7 发布了第 6 个预览版。

主要变化包括:

  • 引入新的类型转换器
  • 更新 System.Formats.Tar API
  • 改进 Generic Math,方便 API 作者的使用
  • 为 ML.NET 引入新的 Text Classification API,增加了最先进的深度学习技术对于自然语言处理
  • 对源代码生成器的多项改进
  • 用于 RegexGenerator 的新 Roslyn 分析器和修复器,以及在 CodeGen、可观察性、JSON 序列化 / 反序列化和使用流方面的多项性能改进

类型转换器

新增用于新添加的原始类型DateOnlyTimeOnlyInt128UInt128Half的公开类型转换器。

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() { }
    }
}

使用示例

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?;

对源代码生成器的多项改进

添加了对IAsyncEnumerable<T> (#59268), JsonDocument(#59954),DateOnly/TimeOnly(#53539) 类型的源代码生成支持。例如:

[JsonSerializable(typeof(typeof(MyPoco))]
public class MyContext : JsonSerializerContext {}

public class MyPoco
{
    // Use of IAsyncEnumerable that previously resulted 
    // in JsonSerializer.Serialize() throwing NotSupportedException 
    public IAsyncEnumerable<int> Data { get; set; } 
}

// It now works and no longer throws NotSupportedException
JsonSerializer.Serialize(new MyPoco { Data = ... }, MyContext.MyPoco); 

改进 Generic Math

.NET 6 发布了预览版的 Generic Math,此特性允许 .NET 开发者在通用代码中利用静态 API,包括运算符。Generic Math 极大地方便了 API 作者,因为他们使用的 API 将开始支持更多类型,而不需要每个数字类型都获得显式支持。

在 .NET 7 中,开发团队对实现进行了改进并响应了社区的反馈。有关更改和可用 API 的更多信息,点此查看

详情查看发布公告

猜你喜欢

转载自www.oschina.net/news/202838/dotnet-7-preview-6-released