【发布时间】:2019-07-03 11:09:22
【问题描述】:
当我尝试在 .NET Core 项目(面向 2.2)中的表达式树上打开 my custom visualizer 时,出现以下异常:
自定义可视化组件在正在调试的进程中引发了“System.TypeLoadException”类型的未处理异常。
无法从程序集“mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”加载类型“System.Runtime.CompilerServices.IsReadOnlyAttribute”。
以下代码(来自问题末尾的堆栈跟踪)似乎有同样的问题:
// using System;
// using System.IO;
// using System.Runtime.Serialization.Formatters.Binary;
var stream = File.Create(Path.GetTempFileName());
var formatter = new BinaryFormatter();
var data = new VisualizerData(expr); // serialized class with information about a given expression tree
formatter.Serialize(stream, data); // fails with the same exception
当代码在 .NET Core 项目中运行,但使用来自引用的 .NET Framework 程序集(针对 4.7.2)的类 (VisualizerData) 时;该程序集具有对 WPF 程序集的引用。
如何调试此问题?可能是什么原因造成的?
请注意,这里没有进行任何反序列化;这都是在启动序列化时完成的。
堆栈跟踪:
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
at System.Reflection.CustomAttribute.IsDefined(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
at System.Runtime.Serialization.SerializationEvents.GetMethodsWithAttribute(Type attribute, Type t)
at System.Runtime.Serialization.SerializationEvents..ctor(Type t)
at System.Runtime.Serialization.SerializationEventsCache.<>c.<GetSerializationEventsForType>b__1_0(Type type)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Runtime.Serialization.SerializationEventsCache.GetSerializationEventsForType(Type t)
at System.Runtime.Serialization.SerializationObjectManager.RegisterObject(Object obj)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, Object data)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, BinaryFormatterWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Boolean check)
at ExpressionTreeVisualizer.VisualizerDataObjectSource.TransferData(Object target, Stream incomingData, Stream outgoingData) in C:\Users\Spitz\source\repos\zspitz\ExpressionToString\Visualizer.Shared\VisualizerDataObjectSource.cs:line 9
at Microsoft.VisualStudio.DebuggerVisualizers.DebuggeeSide.Impl.ClrCustomVisualizerDebuggeeHost.TransferData(Object visualizedObject, Byte[] uiSideData)
【问题讨论】:
-
序列化和反序列化必须使用相同的类和相同版本的网络库(可能对象大小在不同的网络版本中会发生变化)。
-
@jdweng 这个场景以前可以工作。我不确定在过去几个月的哪个时间点发生了这种情况。
-
可能当您更改类或更新网络库时。尝试序列化新数据,然后反序列化以确保它适用于当前配置。
-
@jdweng 序列化和反序列化必须使用相同的类和相同版本的网络库运行,除了docs 说这不应该是一个问题 - - 已定义的类型集保证在 .NET Framework 4.5.1 及更高版本和 .NET Core 2.0 及更高版本之间可序列化。
-
文档是指相同的 c# 类和相同的 4.5.1 和 Core 2.0。但当您更改 c# 类或使用 4.5.0 和 Core 2.0 时不会。
标签: c# .net serialization .net-core