简体   繁体   English

从 .NET v4.7.2 切换到 .NET 标准 v2.0 时,在 DefaultContractResolver 中找不到 IgnoreSerializableAttribute

[英]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.

相关问题 在 .Net Core 项目中使用 .Net Framework dll (v4.7.2) - Use .Net Framework dll (v4.7.2) in .Net Core project WPF.Net v4.7.2 令人担忧的调试和直接运行的区别 - WPF .Net v4.7.2 worrying difference between running in debug and direct 我们可以有一个内置的.Net V2.0引用另一个内置的.Net V4.5吗? - Can we have a DLL built in .Net V2.0 references another built in .Net V4.5? Ranorex 构建失败 9.4.1 找不到框架“.NETFramework,Version=v4.7.2”的参考程序集 - Ranorex build fails 9.4.1 The reference assemblies for framework “.NETFramework,Version=v4.7.2” were not found .Net Framework 4.7.2 引用.Net Standard 2.0 项目 - .Net Framework 4.7.2 Referencing .Net Standard 2.0 project .NET Standard 2.0 Class 库能否依赖于 .NET Framework 4.7.2? - Can .NET Standard 2.0 Class Library depend on .NET Framework 4.7.2? EntityFramwork 与 .net 框架 4.7.2 项目和 .net 标准 2.0 项目的兼容性 - EntityFramwork compatibility with .net framework 4.7.2 projects and .net standard 2.0 project Mime类型的.tif / pdf文件已损坏,无法打开.NET V2.0 - Mime-type .tif/pdf files are corrupted and will not open .NET V2.0 使用Autofac的ASP.NET Core v2.0的SignalR依赖注入 - SignalR Dependency Injection for ASP.NET Core v2.0 using Autofac OAuth v2.0与ASP.NET MVC 4 Web API结合使用 - OAuth v2.0 in combination with ASP.NET MVC 4 Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM