从引用的 .NET Framework 项目序列化值类型期间 .NET Core 项目中的 TypeLoadException [英] TypeLoadException in .NET Core project during serialization of value type from referenced .NET Framework project

查看:31
本文介绍了从引用的 .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 Framework 项目中,我定义了以下类型:

      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 项目中序列化此类型时:

      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,版本=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.

      我该如何解决这个问题?

      (这是我上一个问题的后续.)

      删除 binobj 文件夹并删除解决方案的 .vs 文件夹没有帮助.

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

      我已在 .NET Core 存储库中提交了问题.>

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

      推荐答案

      这可能已在 .NET Core 3 中修复,但在此之前,可以使用以下解决方法:

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

      • 避免自动实现的属性;改用完整的属性或公共字段.(为了抑制编译器警告,我使用了 [SuppressMessage("", "IDE0032", Justification = "https://github.com/dotnet/core/issues/2981")] for这个目的.
      • 在同一个程序集或一些公共程序集中定义自定义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 = "https://github.com/dotnet/core/issues/2981")] for this purpose.
      • Define a custom System.Runtime.CompilerServices.IsReadOnlyAttribute in the same assembly or some common assembly.

      关于 Roslyn 问题的反馈

      这篇关于从引用的 .NET Framework 项目序列化值类型期间 .NET Core 项目中的 TypeLoadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆