I don't know how to deserialize multiples objects from a binary file - help!
How to deserialize the objects (stored in a binary file) and that this files allow in the original state (at design time).?
I need help in this point.![]()
Regards![]()
How to deserialize the objects (stored in a binary file) and that this files allow in the original state (at design time).?
I need help in this point.![]()
Regards![]()
I create a complex datastructure to my liking
Fill the datastructure with whatever.
I create a dictionary(Type Integer, Datastructure) and fill the dictionary
I then serialize the dictionary itself
Deserialization consists of reading the binary file into a new dictionary. Imports
A code example is below:
Imports
System.Runtime.SerializationImports
System.Runtime.Serialization.Formatters.BinaryImports
System.Windows.Forms<Serializable()>
Public Class clsIO 'Class for serializable IO<Serializable()>
Public Structure psec ' This is an example of the datastructure Dim Image As Image Dim Name As String Dim FileSpec As String Dim CommandModifier As String Dim Visible As Boolean End Structure Protected sOriginalDirectory As String = System.Environment.CurrentDirectory Public Structure ImgIndxAndToolTip Dim Status As Long Dim ImageIndex As String Dim Tooltip As String End Structure Protected Const cSeparator As String = "Separator" Public ItemDB As New Dictionary(Of Integer, psec)() Protected packet As New psec Protected bSeparator As BooleanDim fsi As New System.IO.FileStream(name, IO.FileMode.Open) ' Open the dictionary in the defaulting directory Dim BinFormatter As New BinaryFormatter() Dim obj As Object Dim i As Integer
ItemDB.Clear()
TryCType(obj, Dictionary(Of Integer, psec))) Catch e As SystemException GoTo Common_exit Finallyobj = BinFormatter.Deserialize(fsi)
ItemDB = (
End Try For i = 0 To ItemDB.Count - 1fsi.Close()
FalsebSeparator =
packet = ItemDB.Item(i)
FrmToolBar.Pb1.Image = packet.Image
If packet.FileSpec <> cSeparator ThenElseFrmToolBar.AddNewToolStipButton1(packet.FileSpec, packet.Image)
End If Next End IfFrmToolBar.AddSeparator()
Common_exit:
End Sub Public Sub WriteMasterDatabase() Dim Outfile As String = GetRTMFilename() Dim Fso As New System.IO.FileStream(Outfile, IO.FileMode.Create) Dim i As Integer Dim btn As New ToolStripButtonItemDB.Clear()
If FrmToolBar.Toolbar.Items.Count > 0 ThenFor i = 0 To FrmToolBar.Toolbar.Items.Count - 1
False TrybSeparator =
Catchbtn = FrmToolBar.Toolbar.Items(i)
TruebSeparator =
packet.Image =
Nothingpacket.FileSpec = cSeparator
packet.Name =
""packet.CommandModifier =
"" End Try If Not bSeparator ThenTrue End IfFrmToolBar.Pb1.Image = btn.Image
packet.Image = btn.Image
packet.FileSpec = btn.Tag()
packet.Name = btn.Name
packet.Visible =
ItemDB.Add(ItemDB.Count, packet)
Next Dim BinFormatter As New BinaryFormatter TryFinallyBinFormatter.Serialize(Fso, ItemDB)
End Try End If End Sub Public Function WriteItemRecord(ByRef RecNum As Integer, ByRef Obj As Object) As BooleanFso.Close()
True TryWriteItemRecord =
CatchItemDB.Add(ItemDB.Count, Obj)
"WriteItemRecord Error: " & Err.Description, MsgBoxStyle.OKOnly)MsgBox(
WriteItemRecord =
False End Try End Function Public Function ReadItemRecord(ByRef RecNum As Integer, ByRef Packet As psec) As BooleanReadItemRecord =
True TryCatchPacket = ItemDB.Item(RecNum)
"ReadItemRecord Error: " & Err.Description, MsgBoxStyle.OKOnly)MsgBox(
ReadItemRecord =
False End Try End Function Public Sub ClearItemDictionary()End Sub Public Function DoesDBFileExist(ByVal Filename As String) As BooleanItemDB.Clear()
End Function Private Function GetRTMFilename() As StringDoesDBFileExist = System.IO.File.Exists(Filename)
Const DBSFilePrefix As String = "ToolBar"
GetRTMFilename = sOriginalDirectory &
"\" & DBSFilePrefix & ".dbs" End FunctionEnd
ClassThanks a lot for your reply.
I have implemented a dictionary into my code (hashtable) and all works fine (serialization and deserialization). When I serialize the dictionary, all objects into it are serialized fine. When I deserialize the object (hashtable) no error is fired. See attached image, in this image the object is showed with all properties with the values that the user sets.

The problem is that i need that the objects become restored in design mode, the user must be able of modify the design, but all code implements for me not works. The objects must be placed into a IDesignerHost (a form) at run-time, but they are placed but i cant see the objects!. I need the deserialized objects at run-time and that the objects have the desing-time behavior (like they were, previous the serialization process), into this hostdesigner.
See the code for serialize and deserialize the object (sorry, the code is in c#):
I'm really elated you could receive some benefit from what I suggested. I find that creating a dictionary of a complex datastructure, loading the dictionary and serializing and deserializing them to be very hardy and a wonderful and dynamic way to deal with serialization. I've even had great success with serializing multiple dictionaries in the same binary file. All you have to do is to respect the order in which you do it.
I am not a .Net Guru. As with all my many years of computer experience, I am self trained. I did have some questions as well as thoughts about your last post and I need to have them answered in order to insure that we are on the same page.
You speak of "designer mode". Would I be correct that what you are referring to is a very dynamic application, an exectuable image running in a particular mode? It's running a particular class (?) and possibly it's a restrictive class to support the dynamism?
If these assumptions are correct... does this class prevent you from referencing other classes? If not, it seems to me that you could do your serialization and deserialization in those classes and bring them into the designer class?
I apologize for being so dense but I'm not familiar with the restrictions of the class to which you are referring. Perhaps you could educate me so I might be so fortunate as to come up with some suggestions?
Thank You a ot for your support.
Ok. I'm developing an application that allows to the user to design id cards. The application have a toolbar that contain all objects (classes) that the user can use for a id card design (e.x., photo, label, textbox, barcode, sign area, chip area, etc.). All this objects must have the behavior that a control has when is placed in a form at design-mode (into the IDE). I need that this controls remain or become placed into the desing area like if this objects are in a design mode (but my application is running), this main that... all objects must be able of change the size, move to another place, change its attributes, etc. Prior of the serialization process, this objects had this behavior.
IOW, my application allows to the user at run-time, works this objects like a developer do it at design-time into the NET2003's IDE.
The problem is that the rootdesigner is not placed for the object. But i can't to serialize a form (like an ISite).
Regards
I found the solution to my problem. I don't know if this is the best way, but sincerely, i lost many time with this issue (thanks to my boss for his patience!
).
I implemented a PropertyDescriptorCollection for get the properties into the deserialized object and subsequently assign the values into this properties to the component (host.CreateComponent) created with the "design-time" behavior. Using a PropertyDescriptor, you can get/set the value in a property into/from a component. The code below , shows how to get the value (using .GetValue() method) into the property "MargenSuperior" from the object m_qlabelcomponent and set to the same property into the object m_customcomponent (that implements IComponent, IDesignerHost, ISite, etc.; that allow use this component in an design mode environment at run-time:
((QLabel)m_customcomponent).MargenSuperior = (int) props["MargenSuperior"].GetValue(m_qlabelcomponent);
See the attached image for more details in the code.
Like you can see in the code, set the Parent property for the component with the RootComponent in the hostdesigner, allow that the control become visible and that have a "design-time" behavior.
/*Get the Parent property*/ propParent = props["Parent"]; /*Set the Parent property to the form:*/ propParent.SetValue(m_customcomponent, host.RootComponent); |
Codify serialization and deserialization is not easy, and sincerely i was very worried because this issue taken many development time.
Thanks to all coders that indifferently gave to me time, code, tips and support.![]()
Regards
PS.
I hope that this code helps to another members in the forum.