VB序列化到C#序列化
我在一家用VB编写程序的公司开始编程。 我将其转换为C#,因为它是一个更干净的代码 在VB中,他们做了如下操作:VB序列化到C#序列化,c#,vb.net,serialization,C#,Vb.net,Serialization,我在一家用VB编写程序的公司开始编程。 我将其转换为C#,因为它是一个更干净的代码 在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)
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
我无法想象这7个值是如何转换和再转换的
如何反序列化从VB编写的C#文件?
希望有人能帮我
编辑:
现在,我在我的ptoject中包含了一个VB-dll项目,包括“旧”代码。
我的C代码调用VB代码中的一个方法,该方法开始反序列化。
但我仍然收到一个例外:
The object of type "System.Collections.Generic.List`1[System.String]" can not be converted to type "System.Collections.Generic.List`1[System.Boolean]".
但是序列化类中的所有参数在我的实际项目中的类型与在纯VB项目中的类型相同
那么是什么导致了这种异常呢
问候
Henrik在VB.net中也禁止将布尔赋值给字符串变量-除非您的前任没有使用Option Strict On。在这种情况下,你的C#rant是指LAS——编码实践是错误的,而不是VB.Net 反序列化类没有简单的方法——我认为最好的方法是Bool属性,当设置这些属性时,它们也会创建字符串变量。
这将适用于xml序列化,我不确定它是否适用于二进制格式化程序。在VB.net中也禁止将布尔赋值给字符串变量-除非您的前任没有使用Option Strict On。在这种情况下,你的C#rant是指LAS——编码实践是错误的,而不是VB.Net 反序列化类没有简单的方法——我认为最好的方法是Bool属性,当设置这些属性时,它们也会创建字符串变量。 这将适用于xml序列化,我不确定它是否适用于二进制格式化程序。如果您坚持“修复”未损坏的内容,我想您将需要反序列化为一个中间“翻译”类,该类的列表定义为Object或Boolean。然后,该类将输出一个Class1对象。如果您坚持“修复”未损坏的对象,我想您需要将其反序列化为一个中间“translation”类,该类的列表定义为object或Boolean。然后该类将输出一个Class1对象
The object of type "System.Collections.Generic.List`1[System.String]" can not be converted to type "System.Collections.Generic.List`1[System.Boolean]".