You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
765 B

using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace BrilliantSightClient.Model.Extension;
public class DefaultValueContractResolver: DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (property.PropertyType.IsValueType &&
Nullable.GetUnderlyingType(property.PropertyType) == null) // 非可空类型
{
property.DefaultValue = Activator.CreateInstance(property.PropertyType);
property.DefaultValueHandling = DefaultValueHandling.Populate;
}
return property;
}
}