
前言
几乎所有.NET序列化程序的实现基础都是反射。下列代码是Newtonsoft.Json的实现:
- protectedvirtualJsonPropertyCreateProperty(MemberInfomember,MemberSerializationmemberSerialization)
- {
- JsonPropertyproperty=newJsonProperty();
- property.PropertyType=ReflectionUtils.GetMemberUnderlyingType(member);
- property.DeclaringType=member.DeclaringType;
- property.ValueProvider=CreateMemberValueProvider(member);
- property.AttributeProvider=newReflectionAttributeProvider(member);
- ......
- }
反射为某些场景提供了强大的功能,但相对于直接编码,在运行性能上较差,例如Newtonsoft.Json就用缓存进行了优化:
- publicvirtualJsonContractResolveContract(Typetype)
- {
- ValidationUtils.ArgumentNotNull(type,nameof(type));
- return_contractCache.Get(type);
- }
而在.NET 6中,为System.Text.Json提供了Source Generator,可以在编译时就生成序列化源代码。
Demo
使用方法非常简单。
只需实现一个继承自JsonSerializerContext的类,并声明JsonSerializable,指定序列化的类型:
- [JsonSerializable(typeof(WeatherForecast))]
- internalpartialclassWeatherForecastContext:JsonSerializerContext
- {
- }
然后,就可以将自动生成的WeatherForecastContext.Default.WeatherForecast对象作为参数用于序列化:
- varstr=JsonSerializer.Serialize(newWeatherForecast
- {
- TemperatureC=Random.Shared.Next(-20,55),
- Summary=Summaries[Random.Shared.Next(Summaries.Length)]
- },WeatherForecastContext.Default.WeatherForecast);
- varobj=JsonSerializer.Deserialize(str,WeatherForecastContext.Default.WeatherForecast);
单步跟踪,可以看到生成的序列化代码如下,
- privatestaticvoidWeatherForecastSerializeHandler(global::System.Text.Json.Utf8JsonWriterwriter,global::WebApplication1.WeatherForecast?value)
- {
- if(value==null)
- {
- writer.WriteNullValue();
- return;
- }
- writer.WriteStartObject();
- writer.WriteNumber(PropName_TemperatureC,value.TemperatureC);
- writer.WriteNumber(PropName_TemperatureF,value.TemperatureF);
- writer.WriteString(PropName_Summary,value.Summary);
- writer.WriteEndObject();
- }
另外,还可以使用JsonSourceGenerationOptionsAttribute对生成的序列化代码进行一定调整,比如属性名大小写:
- [JsonSourceGenerationOptions(PropertyNamingPolicy=JsonKnownNamingPolicy.CamelCase)]
- [JsonSerializable(typeof(WeatherForecast))]
- internalpartialclassWeatherForecastContext:JsonSerializerContext
- {
- }
结论
在编译时生成源代码可为.NET应用程序带来许多好处,包括提高性能。官方提供的测试结果表明提高了接近40%,有兴趣的朋友可以验证一下:

原文地址:https://www.toutiao.com/a7050079625048752673/








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。