title: TypeLoadException during serialization
body: <p>When I try to open <a href="https://github.com/zspitz/ExpressionToString#visual-studio-debugger-visualizer-for-expression-trees" rel="nofollow noreferrer">my custom visualizer</a> on an expression tree in a .NET Core project (targeting 2.2), I get the following exception:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.TypeLoadException' was thrown by the custom visualizer component in the process being debugged.</p>
    
    <p>Could not load type 'System.Runtime.CompilerServices.IsReadOnlyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.</p>
  </blockquote>
  
  <p>The following code (derived from the stack trace, at the end of the question) appears to have the same issue:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>// 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
  </code></pre>
  
  <p>when the code is running in a .NET Core project, but uses a class (<code>VisualizerData</code>) from a referenced .NET Framework assembly (targeting 4.7.2); that assembly has references to WPF assemblies.</p>
  
  <p><strong>How can I debug this issue? What might cause it?</strong></p>
  
  <p>Note that there isn't any deserialization going on here; this is all while starting up serialization.</p>
  
  <p><a href="https://github.com/zspitz/ExpressionToString/blob/master/Visualizer.Shared/VisualizerDataObjectSource.cs#L9" rel="nofollow noreferrer">Source code for <code>VisualizerDataObjectSource.TransferData(Object target, Stream incomingData, Stream outgoingData) in VisualizerDataObjectSource.cs:line 9</code></a></p>
  
  <hr>
  
  <p>Stack trace:</p>
  
  <pre class="lang-none prettyprint-override"><code>   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&amp; lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType&amp; attributeType, IRuntimeMethodInfo&amp; ctor, Boolean&amp; ctorHasParameters, Boolean&amp; 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.&lt;&gt;c.&lt;GetSerializationEventsForType&gt;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)
  </code></pre>