且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET Web API 和 [Serializable] 类

更新时间:2023-02-15 19:48:31

你只需要这个单行代码就可以让 Json.NET 再次忽略 [Serializable] 语义:

You just need this one-liner to get Json.NET to ignore the [Serializable] semantics again:

((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;

对您来说更好的解决方案可能是完全摆脱 [Serializable],停止使用 BinaryFormatter,并使用不同的序列化程序来执行您想做的任何缓存,例如 Json.NET 序列化程序.

A better solution for you might be to get rid of [Serializable] altogether, stop using BinaryFormatter, and use a different serializer instead to do whatever caching you want to do, like the Json.NET serializer for example.