从引用的.NET Framework项目对值类型进行序列化期间.NET Core项目中的TypeLoadException

编程入门 行业动态 更新时间:2024-10-23 09:41:26
本文介绍了从引用的.NET Framework项目对值类型进行序列化期间.NET Core项目中的TypeLoadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我具有以下项目依赖项结构:

I have the following project dependency structure:

  • _visualizerTests.dotnetcore - - .NET Core 2.2
    • Visualizer.2019 - .NET Framework 4.7.2
    • _visualizerTests.dotnetcore -- .NET Core 2.2
      • Visualizer.2019 -- .NET Framework 4.7.2

      在.NET中框架项目,我定义了以下类型:

      In the .NET Framework project, I have the following type defined:

      [Serializable] public struct EndNodeData { public string Closure { get; set; } public string Name { get; set; } public string Type { get; set; } public string Value { get; set; } }

      当我尝试在.NET Core项目中序列化此类型时: / p>

      When I try to serialize this type in the .NET Core project:

      var endnodeData = new EndNodeData { Closure = null, Name = null, Type = "int", Value = "5" }; var stream = File.Create(Path.GetTempFileName()); var formatter = new BinaryFormatter(); formatter.Serialize(stream, endnodeData);

      我遇到以下异常:

      System.TypeLoadException:'无法从程序集'mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089加载类型'System.Runtime.CompilerServices.IsReadOnlyAttribute' '。'

      System.TypeLoadException: 'Could not load type 'System.Runtime.CompilerServices.IsReadOnlyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'

      .NET Framework项目中定义的类类型可以无问题地序列化,.NET Core中定义的值类型也可以项目。

      Class types defined in the .NET Framework project serialize without a problem, as do value types defined in the .NET Core project.

      我该如何解决?

      (请关注-直到我的上一个问题。)

      (This is a follow-up to my previous question.)

      删除 bin 和 obj 文件夹,并删除解决方案的 .vs 文件夹没有帮助。

      Deleting the bin and obj folders, and deleting the solution's .vs folder doesn't help.

      我已经提交了问题。

      I've filed an issue on the .NET Core repo.

      推荐答案

      Thi s可能已在.NET Core 3中修复,但在此之前,可以使用以下变通办法:

      This has presumably been fixed in .NET Core 3, but until then, the following workarounds are available:

      • 避免自动实现的属性;使用完整属性或公共字段。 (为了抑制编译器警告,我使用了 [SuppressMessage(, IDE0032,Justification = github/dotnet/core/issues/2981\")]。
      • 在同一程序集或某些通用程序中定义自定义 System.Runtime.CompilerServices.IsReadOnlyAttribute
      • Avoid auto-implemented properties; use full properties, or public fields instead. (In order to suppress the compiler warning, I used [SuppressMessage("", "IDE0032", Justification = "github/dotnet/core/issues/2981")] for this purpose.
      • Define a custom System.Runtime.CompilerServices.IsReadOnlyAttribute in the same assembly or some common assembly.

      对罗斯林问题的反馈

更多推荐

从引用的.NET Framework项目对值类型进行序列化期间.NET Core项目中的TypeLoadException

本文发布于:2023-11-03 12:40:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   类型   序列化   Framework   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!