问题
I am creating a vb.net application which needs to be able to deserialize files that were serialized in a different application as well as serialize files that can be deserialized in that other application.
I am trying to make this work via the use of a SerializationBinder to convert the type during serialize / deserialize. Using This article from MSDN as a reference, this is what I have at this point....
In my deserialize function:
myDeserializer.Binder = New TypeConverter()
openCount = DirectCast(myDeserializer.Deserialize(stream), Count)
Then:
Class TypeConverter
Inherits SerializationBinder
Public Overrides Function BindToType(assemblyName As String, typeName As String) As Type
Dim returnType As Type = Nothing
If assemblyName = "[name from other application]" Then
assemblyName = Assembly.GetExecutingAssembly().FullName
End If
If typeName = "[other application namespace].Count" Then
typeName = "Count"
End If
returnType = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName))
Return returnType
End Function
End Class
The If statements are true, so assemblyName and typeName are getting set correctly. But returnType is not getting set - it remains Nothing. And I have no idea why.
Note that I do have all the same classes in both applications (namely Count, which is the type that is serialized / deserialized between them).
Thank you!
回答1:
OK, well it turns out that in this case (using Type.GetType in this use), it doesn't work unless you specify the namespace before the type. So where I had typeName = "Count" I had to change that to typeName = "[my project].Count". https://msdn.microsoft.com/en-us/library/a87che9d%28v=vs.110%29.aspx
It also turns out that this function repeats for every type coming in (not just specifically what is being deserialized, but for every type of the properties in that). So being that my "Count" type contains properties that are of other custom types, and also some that are lists of other custom types, I had to make sure each of these is getting properly converted.
Where I am at now is here: in stepping through the process, it looks like all goes fine. But after it has converted the first type that is a list of a custom type's structure, then upon trying to convert the next one I get an Argument Exception.
This is the code now:
Public Overrides Function BindToType(assemblyName As String, typeName As String) As Type
If assemblyName.Contains("[other project]") Then
assemblyName = Assembly.GetExecutingAssembly().FullName
End If
If typeName.Contains("System.Collections.Generic.List`1[[[Other Project].[Type],") Then
typeName = String.Format("System.Collections.Generic.List`1[[[my project].[type]], {0}", Assembly.GetExecutingAssembly().FullName)
ElseIf typeName.Contains("System.Collections.Generic.List`1[[[Other Project].[type]+[structure],") Then
typeName = String.Format("System.Collections.Generic.List`1[[[My project].[Type]+[Structure], {0}", Assembly.GetExecutingAssembly().FullName)
ElseIf typeName.Contains("[Other Project]") Then
typeName = Replace(typeName, "[Other Project]", "[My Project]")
End If
Return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName))
End Function
So the System.Collections.Generic.List`1[[[My project].[Type]+[Structure] is a list of a custom type's structure. The first one that it comes to seems to convert fine, but then when trying to convert whatever is next, I get this:
Additional information: Object of type 'System.Runtime.Serialization.TypeLoadExceptionHolder' cannot be converted to type 'System.Collections.Generic.List`1[My Project.Type+Structure]'.
The Type and Structure in this case being the type that was previously converted...not the type that is currently being converted when this exception is thrown.
No idea what is going on here....
来源:https://stackoverflow.com/questions/31521389/type-gettype-returns-nothing-in-serializationbinder