Bug 1112 - System.Runtime.Serialization.Formatters.Binary Could not find type generics with serialization binder
Summary: System.Runtime.Serialization.Formatters.Binary Could not find type generics w...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2011-09-28 15:28 UTC by Dan
Modified: 2015-03-20 05:52 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Dan 2011-09-28 15:28:53 UTC
When using a custom serialization binder for generic types that contain types that have changed such as:

System.Collections.Generic.List`1[[Namespace.sub.Class, Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

I created a Serialization binder as below to handle this and map it to the current version of the same class.

The problem is (as far as I can tell from 2.6.4 source as a reference) is that in:

System.Runtime.Serialization.Formatters.Binary.ObjectReader

In Function: ReadType(BinaryReader reader, TypeTag code, bool throwOnError)

It gets considered a runtime type and throws the exception:

Could not find type 'System.Collections.Generic.List`1[[Namespace.sub.Class, Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.  
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadType (System.IO.BinaryReader reader, TypeTag code) [0x00000] in <filename unknown>:0    
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadTypeMetadata (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo) [0x00000] in <filename unknown>:0    
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectInstance (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in <filename unknown>:0    
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in <filename unknown>:0    
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in <filename unknown>:0    
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in <filename unknown>:0    
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in <filename unknown>:0   
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <filename unknown>:0    

This code path never checks the binder before throwing that exception. While I can understand why that decision was made, this works in .Net 2.0.
From what I can tell a simple (and non informed) way to fix this would be to check with the binder in ReadType before the exception is thrown.

Is there a way to avoid this in the mean time?

Thanks,

Dan

Code Below
============

public class DictionarySerializationBinder : SerializationBinder
	{
		public override Type BindToType(string assemblyName, string typeName)
		{
			Type typeToReturn;
			if (_typeDictionary.TryGetValue(typeName, out typeToReturn))
			{
				return typeToReturn;
			}
			else
			{
				return null;
			}
		}

		internal void AddBinding(string fromTypeName, Type toType)
		{
			if (toType == null)
			{
				throw new ArgumentNullException("toType");
			}
			_typeDictionary.Add(fromTypeName, toType);

		}

		Dictionary<string, Type> _typeDictionary = new Dictionary<string, Type>();
}


DictionarySerializationBinder binder = new DictionarySerializationBinder();

binder.AddBinding("System.Collections.Generic.List`1[[Namespace.sub.Class, Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]", typeof(List<Namespace.sub.Class>));
Comment 1 Marek Safar 2015-03-20 05:52:17 UTC
Fixed in master