[英]IgnoreSerializableAttribute not found in DefaultContractResolver when switching from .NET v4.7.2 to .NET Standard v2.0
I'm in the process of migrating one of our libraries from .NET v4.7.2 to .NET Standard v2.0.我正在将我们的一个库从 .NET v4.7.2 迁移到 .NET Standard v2.0。
This code compiles when using .NET v4.7.2此代码在使用 .NET v4.7.2 时编译
var resolver = new DefaultContractResolver
{
IgnoreSerializableAttribute = true,
NamingStrategy = new CamelCaseNamingStrategy(true, true),
};
but when using .NET Standard it does not.但是当使用 .NET 标准时,它不会。 Apparently IgnoreSerializableAttribute is not part of DefaultContractResolver in v9.01 of Newtonsoft.Json when using .NET Standard.
在使用 .NET 标准时,显然 IgnoreSerializableAttribute 不是 Newtonsoft.Json v9.01 中 DefaultContractResolver 的一部分。
I'm using v9.01 of Newtonsoft.Json as we're still using v1.0 Azure functions and they only work with v9.01.我正在使用 Newtonsoft.Json 的 v9.01,因为我们仍在使用 v1.0 Azure 功能,它们仅适用于 v9.01。
Can anyone explain what's happening and why there's a difference between Newtonsoft.Json v9.01 in .NET v4.7.2 and .NET Standard v2.0?谁能解释发生了什么以及为什么 .NET v4.7.2 和 .NET Standard v2.0 中的 Newtonsoft.Json v9.01 之间存在差异?
You do not need to set IgnoreSerializableAttribute
to true
because it is true
by default.您不需要将
IgnoreSerializableAttribute
设置为true
,因为它默认为true
。 This can be seen in the reference source for DefaultContractResolver
:这可以在
DefaultContractResolver
的参考源中看到:
public DefaultContractResolver() { #if HAVE_BINARY_SERIALIZATION IgnoreSerializableAttribute = true; #endif #pragma warning disable 618 DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public; #pragma warning restore 618 _contractCache = new ThreadSafeStore<Type, JsonContract>(CreateContract); }
Thus on all versions of Json.NET, the following code will create a contract resolver that ignores the [Serializable]
attribute:因此,在 Json.NET 的所有版本上,以下代码将创建一个忽略
[Serializable]
属性的合约解析器:
var resolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy(true, true),
};
To answer your specific question as to why IgnoreSerializableAttribute
is not present in Json.NET v9.0.1 when targeting .NET Standard 2.0 , it is because v9.0.1 actually only targets .NET Standard 1.0, and Microsoft's System.SerializableAttribute
did not exist in .NET Standard 1.0. To answer your specific question as to why
IgnoreSerializableAttribute
is not present in Json.NET v9.0.1 when targeting .NET Standard 2.0 , it is because v9.0.1 actually only targets .NET Standard 1.0, and Microsoft's System.SerializableAttribute
did not exist in .NET Standard 1.0. If I recall, MSFT introduced it into .NET Standard 2.0 as part of the partial back-port of binary serialization (now deprecated ).如果我记得,MSFT 将它引入 .NET 标准 2.0 作为二进制序列化的部分反向端口(现已弃用)的一部分。 When Json.NET added support for .NET Standard 2.0 in release 11 , support for
IgnoreSerializableAttribute
got included.当 Json.NET 在 版本 11中添加对 .NET 标准 2.0 的支持时,包括对
IgnoreSerializableAttribute
的支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.