从引用的.NET Framework项目对值类型进行序列化期间.NET Core项目中的TypeLoadException [英] TypeLoadException in .NET Core project during serialization of value type from referenced .NET Framework project
问题描述
我具有以下项目依赖项结构:
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.2Visualizer.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
andobj
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 = https://github.com/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 = "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.
这篇关于从引用的.NET Framework项目对值类型进行序列化期间.NET Core项目中的TypeLoadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-