Blazor 项目中的反序列化问题

Problem with deserialization in Blazor project

当我尝试将 JSON 字符串反序列化为 class 时出现异常, 我用 Newtonsoft.Json

我是这样做的:

MyClass response = JsonConvert.DeserializeObject<MyClass>(JsonString);

在 C# .net Core API 项目中使用相同数据的相同代码 Newtonsoft.Json 工作正常,但在 Blazor 项目中有异常

blazor.webassembly.js:1 WASM: Unhandled Exception: blazor.webassembly.js:1 WASM: System.TypeLoadException: Could not load type of field 'Newtonsoft.Json.Linq.JToken:_parent' (1) due to: Could not resolve type with token 010000f5 from typeref (expected class 'System.Collections.Specialized.INotifyCollectionChanged' in assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e') assembly:mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e type:System.Collections.Specialized.INotifyCollectionChanged member:(null)

看起来是这个错误https://github.com/aspnet/Blazor/issues/1692

您需要禁用链接https://blazor.net/docs/host-and-deploy/configure-linker.html#disable-linking-with-an-msbuild-property

<PropertyGroup>
  <BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>