简体   繁体   English

VB序列化到C#序列化

[英]VB Serialisation to C# Serialisation

I started programming in a Company where a program was written in VB. 我在一家用VB编写程序的公司开始编程。 I converted it to C# because it's a much cleaner code. 我将其转换为C#,因为它是更简洁的代码。

In VB they did something like that: 在VB中,他们做了这样的事情:

    Class Program
        Friend Shared Sub Main(args As String())
        Dim obj As New Class1()
        Dim fs As New System.IO.FileStream("test.txt", System.IO.FileMode.OpenOrCreate)
        Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            bf.Serialize(fs, obj)
            fs.Close()

            fs = New System.IO.FileStream("test.txt", System.IO.FileMode.OpenOrCreate)
            bf = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            obj = bf.Deserialize(fs)
            For Each item As String In obj.myList
                Console.WriteLine(item)
            Next
            Console.Read()
        End Sub
    End Class

    <Serializable> _
    Class Class1
        Public myList As List(Of String)
        Public Sub New()
            myList = New List(Of String)()
            myList.Add(True)
            myList.Add(False)
            myList.Add(True)
            myList.Add(False)
            myList.Add(True)
            myList.Add(False)
            myList.Add(True)
        End Sub
    End Class

But in C# the assignment of a bool to a String-Var is of course forbidden. 但是在C#中,绝对不允许将布尔值分配给String-Var。 I wrote a class in C# that is named Class1Old which includes all members as declared in VB, but when I deserialize in C# I catch the exception "String can not be converted in Boolean". 我在C#中编写了一个名为Class1Old的类,该类包括VB中声明的所有成员,但是当我在C#中反序列化时,我捕获到异常“字符串无法转换为布尔值”。

I watched in the written *.txt file and the result was very confusing: 我看着书面的* .txt文件,结果非常令人困惑:

        ÿÿÿÿ          JConsoleApplication1, Version=1.0.0.0, Culture=neutral,  
    PublicKeyToken=null   
    ConsoleApplication1.Class1   
    myListSystem.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
    System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
    _items_size_version                       True   False                     

I can't imagine how theese 7 values are converted and reconverted. 我无法想象这7个值是如何转换和重新转换的。

How can I deserialize the file in C# which was written from VB? 我如何反序列化从VB编写的C#文件?

hope somebody can help me... 希望有人可以帮助我...

EDIT: 编辑:

Now I include a VB dll-project to my ptoject including the "old" code. 现在,我将一个包含“旧”代码的VB dll项目添加到了我的项目中。 my C#-code calls a method in the VB-code which starts deserializing. 我的C#代码在VB代码中调用了一个方法,该方法开始反序列化。 But I still receive the ArgumentException: 但是我仍然收到ArgumentException:

The object of type "System.Collections.Generic.List`1[System.String]" can not be converted to type "System.Collections.Generic.List`1[System.Boolean]".

But all arguments in the serialized class are the same type in my actual project as in my pure VB project. 但是,在我的实际项目中,序列化类中的所有参数与纯VB项目中的参数类型相同。

So what das cause that exception?? 那么,是什么原因导致异常呢?

greets Henrik 问候亨里克

The assignment of a bool to a string variable is also forbidden in VB.net - unless your predecessor did not use Option Strict On. 在VB.net中也禁止将布尔值分配给字符串变量-除非您的前任未使用Option Strict On。 In that case your C# rant is meaninglas - the coding practice was at fault, not VB.Net. 在这种情况下,您的C#话语就是意思-编码实践有问题,而不是VB.Net。

There is no easy way to deserialize the class - I think the best way would be Bool Properties which, when set, are also creating the string variables. 没有简单的方法来反序列化该类-我认为最好的方法是Bool Properties,该布尔属性在设置时也会创建字符串变量。 This would work with xml serialiazation, I am not sure that if it works with the Binary formatter. 这将与xml serialiazation一起使用,我不确定是否与Binary格式化程序一起使用。

If you insist on "fixing" something that isn't broken, I suppose you will need to deserialize into an intermediate "translation" class that has the list defined as Object or Boolean. 如果您坚持要“修复”未损坏的内容,我想您将需要反序列化为一个中间的“翻译”类,该类的列表定义为“对象”或“布尔”。 This class will then output a Class1 object. 然后,此类将输出Class1对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM