Type.GetType在SerializationBinder中不返回任何内容

Type.GetType在SerializationBinder中不返回任何内容

问题描述:

我正在创建一个vb.net应用程序,该应用程序需要能够反序列化在其他应用程序中序列化的文件以及能够序列化在其他应用程序中反序列化的文件.

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.

我正在尝试通过使用SerializationBinder在序列化/反序列化期间转换类型来完成这项工作.使用 MSDN上的这篇文章作为参考,这是我目前所拥有的....

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)

然后:

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

If语句为true,因此正确设置了assemblyName和typeName.但是returnType尚未设置-它仍然为Nothing.而且我不知道为什么.

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.

请注意,我在两个应用程序中都具有所有相同的类(即Count,这是在它们之间进行序列化/反序列化的类型).

Note that I do have all the same classes in both applications (namely Count, which is the type that is serialized / deserialized between them).

谢谢!

好吧,事实证明,在这种情况下(在此用法中使用Type.GetType),除非在类型之前指定了名称空间,否则它将不起作用.因此,在我有 typeName ="Count" 的地方,我不得不将其更改为 typeName ="[my project] .Count" . https://msdn.microsoft.com/en-us/library/a87che9d%28v = vs.110%29.aspx

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

结果还表明,此函数会针对传入的每种类型(不仅是要反序列化的内容,而且还会针对其中的每种类型的属性)重复执行此操作.因此,由于我的"Count"类型包含其他自定义类型的属性,并且还包含其他自定义类型的列表,因此我必须确保每个属性都已正确转换.

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.

我现在的位置在这里:在逐步执行过程中,一切似乎都很好.但是,在它转换了第一个类型后,即一个自定义类型的结构列表,然后尝试转换下一个类型时,我得到了Argument Exception.

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.

现在这是代码:

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

因此, System.Collections.Generic.List`1 [[[我的项目].[Type] + [Structure] "是自定义类型结构的列表.涉及到的第一个似乎可以很好地转换,但是随后尝试转换下一个内容时,我得到了:

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:

其他信息:类型的对象'System.Runtime.Serialization.TypeLoadExceptionHolder'不能为转换为类型'System.Collections.Generic.List`1 [My Project.Type + Structure]'.

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....