在最新版本的JSON.NET for Unity3D上,我收到以下编译器警告消息:
[CS0618] `Newtonsoft.Json.Serialization.DefaultContractResolver.DefaultMembersSearchFlags' is obsolete:
`DefaultMembersSearchFlags is obsolete. To modify the members serialized inherit from DefaultContractResolver and override the GetSerializableMembers method instead.'
GetSerializableMembers方法的签名是:
protected virtual List<MemberInfo> GetSerializableMembers(Type objectType)
我很困惑如何覆盖此方法来完成我们在简单设置DefaultMembersSearchFlags时所拥有的相同逻辑等效项。我们之前需要进入GetSerializableMembers覆盖的用法是:
contractResolver = new DefaultContractResolver() {
DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic
};
是否存在此方法的工作覆盖示例?谷歌让我失望。
到目前为止,我通过对DefaultContractResolver.cs中的DefaultMembersSearchFlags的当前使用进行逆向工程来尝试:
private class ContractResolver : DefaultContractResolver {
override protected List<MemberInfo> GetSerializableMembers(Type objectType) {
BindingFlags DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
return ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
}
}
问题是...... ReflectionUtils 似乎无法从当前的JSON.NET包中导入,并且滚动我自己的所有听起来像兔子踪迹的版本,这应该是'容易' ......对吗?