Saturday, August 22, 2020
All About Serializing in Visual Basic
About Serializing in Visual Basic Serialization is the way toward changing over an article into a direct succession of bytes called a byte stream. Deserialization just switches the procedure. However, for what reason would you need to change over an item into a byte stream? The principle reason is so you can move the item around. Think about the conceivable outcomes. Since everything is an article in .NET, you can serialize anything and spare it to a record. So you could serialize pictures, information documents, the present condition of a program module (state resembles a depiction of your program at a point in time so you could briefly suspend execution and start again later) ... whatever you have to do. You can likewise store these items on plate in records, send them over the web, pass them to an alternate program, keep a reinforcement duplicate for wellbeing or security. The potential outcomes are truly huge. That is the reason serialization is such a key procedure in .NET and Visual Basic. The following is a segment on custom serialization by actualizing the ISerializable interface and coding a New and a GetObjectData subroutine. As a first case of serialization, lets do perhaps the least demanding system, yet in addition one of the most helpful: serializing information, and afterward deserializing information in straightforward class to and from a record. In this model, the information isn't just serialized, however the structure of the information is spared as well. The structure here is pronounced in a module to keep things ... well ... organized. Module SerializeParmsSerializable() Public Class ParmExampleà à à Public Parm1Name As String Parm1 Nameà à à Public Parm1Value As Integer 12345à à à Public Parm2Name As Stringà à à Public Parm2Value As DecimalEnd ClassEnd Module At that point, singular qualities can be spared to a document this way: Imports System.Runtime.Serialization.Formatters.BinaryImports System.IOPublic Class Form1à à à Private Sub mySerialize_Click( _à à à à à à ByVal sender As System.Object, _à à à à à à ByVal e As System.EventArgs) _à à à à à à Handles mySerialize.Clickà à à à à à Dim ParmData As New ParmExampleà à à à à à ParmData.Parm2Name Parm2 Nameà à à à à à ParmData.Parm2Value 54321.12345à à à à à à Dim s As New FileStream(ParmInfo, FileMode.Create)à à à à à à Dim f As New BinaryFormatterà à à à à à f.Serialize(s, ParmData)à à à à à à s.Close()à à à End SubEnd Class What's more, those equivalent qualities can be recovered this way: Imports System.Runtime.Serialization.Formatters.BinaryImports System.IOPublic Class Form1à à à Private Sub myDeserialize_Click( _à à à à à à ByVal sender As System.Object, _à à à à à à ByVal e As System.EventArgs) _à à à à à à Handles myDeserialize.Clickà à à à à à Dim s New FileStream(ParmInfo, FileMode.Open)à à à à à à Dim f As New BinaryFormatterà à à à à à Dim RestoredParms As New ParmExampleà à à à à à RestoredParms f.Deserialize(s)à à à à à à s.Close()à à à à à à Console.WriteLine(RestoredParms.Parm1Name)à à à à à à Console.WriteLine(RestoredParms.Parm1Value)à à à à à à Console.WriteLine(RestoredParms.Parm2Name)à à à à à à Console.WriteLine(RestoredParms.Parm2Value)à à à End SubEnd Class A Structure or an assortment, (for example, an ArrayList) instead of a Class could likewise be serialized to a document this equivalent way. Since we have gone over the essential serializing process, lets take a gander at the particular subtleties that are a piece of the procedure on the following page. One of the primary things you should see about this model is the Serializable() quality in the Class. Qualities are simply more data that you can give to VB.NET about an article and theyre utilized for many things.à The characteristic in this code advises VB.NET to include additional code so later on, everything in this class can be serialized. In the event that there are explicit things in the Class that you dont need to be serialized, you can utilize the NonSerialized() ascribe to bar them: NonSerialized() Public Parm3Value As String Whatever In the model, notice is that Serialize and Deserialize are strategies for the BinaryFormatter object (f in this model). f.Serialize(s, ParmData) This item takes the FileStream object and the article to be serialized as parameters. Well observe that VB.NET offers another item that permits the outcome to be communicated as XML. Furthermore, one last note, if your item incorporates other subordinate articles, theyll be serialized as well! However, since all articles that are serialized must be set apart with the Serializable() property, these kid objects must be denoted that way as well. Just to be totally clear about what's going on in your program, you should show the document named ParmData in Notepad to perceive what serialized information resembles. (In the event that you followed this code, it ought to be in the bin.Debug organizer in your task.) Since this is a parallel document, the greater part of the substance isnt intelligible content, yet you ought to have the option to perceive any strings in your serialized record. Well do a XML form straightaway and you should contrast the two just with know about the distinction. Serializing to XML rather than a twofold record requires not many changes. XML isnt as quick and cant catch some article data, however its unmistakably progressively adaptable. XML can be utilized by pretty much some other programming innovation on the planet today. In the event that you need to be certain your document structures dont tie you into Microsoft, this is a decent choice to investigate. Microsoft is stressing LINQ to XML to make XML information records in their most recent innovation yet numerous individuals despite everything incline toward this technique. The X in XML represents eXtensible. In our XML model, were going to utilize one of those augmentations of XML, an innovation called SOAP. This used to mean Simple Object Access Protocol however now its only a name. (Cleanser has been overhauled so much that the first name doesnt fit that well any longer.) The primary concern that we need to change in our subroutines is the declation of the serialization formatter. This must be changed in both the subroutine that serializes the article and the one that deserializes it once more. For the default design, this includes three changes to your program. To begin with, you need to add a Reference to the task. Right-click the task and select Add Reference .... Ensure ... System.Runtime.Serialization.Formatters.Soap ... has been added to the undertaking. At that point change the two articulations in the program that references it. Imports System.Runtime.Serialization.Formatters.SoapDim f As New SoapFormatter This time, in the event that you look at the equivalent ParmData document in Notepad, youll see that the entire thing is in clear XML content, for example, ... Parm1Name idref-3Parm1 Name/Parm1NameParm1Value12345/Parm1ValueParm2Name idref-4Parm2 Name/Parm2NameParm2Value54321.12345/Parm2Value There is likewise a great deal of extra XML there that is vital for the SOAP standard in the record also. In the event that you need to confirm what the NonSerialized() quality does, you can include a variable with that characteristic and take a gander at the document to check that its excluded. The model we just coded just serialized the information, however assume you have to control how the information is serialized. VB.NET can do that as well! To achieve this, you have to get somewhat more profound into the idea of serialization. VB.NET has another item to assist here: SerializationInfo. In spite of the fact that you can code custom serialization conduct, it accompanies an expense of additional coding. The essential additional code is demonstrated as follows. Keep in mind, this class is utilized rather than the ParmExample class appeared in the prior model. This isnt a total model. The object is to show you the new code that is required for custom serialization. Imports System.Runtime.SerializationSerializable() _Public Class CustomSerializationà à à Implements ISerializableà à à information to be serialized hereâ â â Public SerializedVariable as Typeà à à Public Sub New()â â â default constructor when the classâ â â is made - custom code can beâ â â included here tooà à à End Subà à à Public Sub New( _à à à à à à ByVal data As SerializationInfo, _à à à à à à ByVal setting As StreamingContext)à à à à à à introduce your program factors fromâ â â â â â a serialized information storeà à à End Subà à à Public Sub GetObjectData( _à à à à à à ByVal data As SerializationInfo, _à à à à à à ByVal setting As StreamingContext) _à à à à à à Implements ISerializable.GetObjectDataà à à à à à update the serialized information storeâ â â â â â from program variablesà à à End SubEnd Class The thought is that now you can (and, indeed, you should) do the entirety of the refreshing and perusing of information in the serialized information store in the New and GetObjectData subroutines. You should likewise incorporate a conventional New constructor (no parameter list) in light of the fact that youre executing an interface. The class will regularly have formal properties and strategies coded too ... Conventional PropertyPrivate newPropertyValue As StringPublic Property NewProperty() As Stringà à à Getà à à à à à Return newPropertyValueà à à End Getà à à Set(ByVal esteem As String)à à à à à à newPropertyValue valueà à à End SetEnd Property Generic MethodPublic Sub MyMethod()à à à method codeEnd Sub The subsequent serialized class can make interesting qualities in the record dependent on the code you gracefully. For instance, a land class may refresh a the worth and address of a house however the class would seria
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.