Scenario Simplification:
The data in the program is obtained every 1s in real time and stored in the list, which needs to be saved to the file in time.
The previous method was to serialize the list object into a file, with the benefit that it did not require much processing and that there were no elements in the chain list (as opposed to the third method in the following). However, there is a problem, each time you have to serialize the entire list into a file, when the data is more expensive after the cost is very large. It should be intuitive to append only the new data to the file at a time.
For brevity, exception handling, local variable declarations, and so on. Each time only the code of the modified class is posted, Measuredvalue is a class that stores the collected data.
The first version of the code is as follows:
1 public static class Fileserializer 2 {3//stream is the caller who is responsible for shutting down the 4 public static void Serialize (FileStream s Tream, Object Objecttoserialize) 5 {6 BinaryFormatter bformatter = new BinaryFormatter (); 7 Bformatter.serialize (stream, objecttoserialize); 8} 9 public static T deserialize<t> (FileStream stream) {T objecttodeserial ize = Default (T); BinaryFormatter Bformatter = new BinaryFormatter (); objecttodeserialize = (T) bformatter.deser Ialize (stream); Return objecttodeserialize;15}16}17 class Serialize18 {public bool Serializemeasuredd ATA (list<measuredvalue> values) {serializehelper data = new Serializehelper (values); Fil Eserializer.serialize (stream, data),}23 public bool Deserializemeasureddata (List<measuredvalue> V Alues) 24 {25 Serializehelper data = null;26 stream = File.Open (_filename, FileMode.Open, FileAccess.Read); 27 data = fileserializer.deserialize<serializehelper> (stream); values = data;29}30 31}32//33//Want to serialize some specified list and add other information when serializing, so add this serializehelper. Serializehelper class () {list<measuredvalue> _values;36 public serializehelper (list<measured Value> values) {PNs _values = values;38}39 public void GetObjectData (SerializationInfo info, StreamingContext Ctxt. AddValue ("VALUES", _values, typeof (List<measuredvalue>));}43 Private Serializehelper (serializatio Ninfo info, StreamingContext context) _values = (list<measuredvalue>) info. GetValue (_values, typeof (List<measuredvalue>)); 46}47 48}
Later, I want to realize that only the increased collection data is serialized into the file at a time. I'm not sure if the idea is right, so use the fast rough method to achieve it first.
The second version of the code is as follows:
1 class Serialize 2 {3 uint _id; 4 public bool Serializemeasureddata (list<measuredvalue> value s) {5 Serializehelper data = new Serializehelper (values, _id); 6 _id++; 7 Fileserializer.serialize (stream, data); 8} 9 public bool Deserializemeasureddata (list<measuredvalue> values) 10 {11 Serializehelper data = null;12 stream = File.Open (_filename, FileMode.Open, FileAccess.Read); 13 data = fileserializer.deserialize<serializehelper> (stream); values = data;15}16 }17 class Serializehelper () {list<measuredvalue> _values;21 _id;22 public Seri Alizehelper (list<measuredvalue> values, uint ID) {_values = values;24 _id = id;25 }26 public void GetObjectData (SerializationInfo info, StreamingContext ctxt) 28{Info. AddValue ("VALUES" +_id. ToString (), _values[_values. Length-1], typeof (Measuredvalue)); 30}31//deserialization ID is maintained by Serializehelper because it is not possible to pass through the parameters. Private Ser Ializehelper (SerializationInfo info, StreamingContext context) {_id = 0;35 Measuredva Lue mv= null;36 _values = new list<measuredvalue> (); PNs try{38 while (true) {39 MV = (Measuredvalue) info. GetValue ("VALUES" +id. ToString (), typeof (Measuredvalue)); _values. ADD (MV);}42}43 catch (Exception ex) {//Show exception description to end of file 44 45 } 46} 47}
However, the test error, not the normal serialization of the linked list. Can you guess where my problem is?
Serializehelper (SerializationInfo info, StreamingContext context) is found in the info variable in this function, MemberCount value is 1, membernames[4] Only Membernames[0] will not be empty, is "VALUES0", so it is miserable, the serializehelper in the deserialization and serialization is corresponding. Since serialization is only written to a single measuredvalue, when deserializing, only one measuredvalue can be obtained, so the operation of generating the list cannot be performed in this class. For Serializationinfo.addvalue (String name, object value, type, type) The function of this parameter, I am very puzzled, it seems that this name is not a global scope identifier, in the above example, is the role of identifiers only in the byte stream that represents a serializehelper?
Originally felt very troublesome, in accordance with my usual straight to the idea, need to list part of the serialization, before the search, there is no information, and I do not know how to organize key words to search. Later I thought that you could encapsulate the ID that must be used into a class, split the list into a single element, serialize each new element whenever it was serialized, and then, when deserializing, restore each element and then assemble it into its corresponding linked list based on the attributes of the element.
A new class TOSERIAMV is added, encapsulating the required listname information and measuredvalue, each time the object is serialized and deserialized.
The third version of the code is as follows:
1 class TOSERIAMV {2 Measuredvalue _value; 3 string _listname; 4 public measuredvalue Value {5 get {return _value;} 6} 7 public String Name 8 {9 get {return _listname;} }11 public TOSERIAMV (measuredvalue value, String listname) {_value = value;14 _listname = listname;15}16}17 class Serializehelper () {const string _name = "Serialized MV "; toseriamv _toserialmv;20 list<measuredvalue> _values; Public Serializehelper (Measuredvalue value,string listname) (_toser IALMV = new TOSERIAMV (value, listname); + Private Serializehelper (SerializationInfo info, streamin Gcontext context) {_deserializedsuccessful = false;29 _TOSERIALMV = (toser IAMV) info. GetValue (_name, typeof (TOSERIAMV));_deserializedsuccessful = true; }33 public void GetObjectData (SerializationInfo info, StreamingContext ctxt) 34 {35 Info. AddValue (_name, _TOSERIALMV, typeof (TOSERIAMV)),}37}38 class Serialize39 {n-public bool Seri Alizemeasureddata (measuredvalue value, String listname) serializehelper data = new Serializehelp ER (value, listname); Fileserializer.serialize (stream, data),}44 public bool Deserializemeasureddata (list<m Easuredvalue> values) {serializehelper data = null;47 TOSERIAMV TOSERIALMV = null;48 stream = File.Open (_filename, FileMode.Open, FileAccess.Read); (Stream. Position<stream. Length) {data = fileserializer.deserialize<serializehelper> (stream); 53 RET = data. GetInfo (out TOSERIALMV); if (Ret==false) break;56 Addtolist (TOSERIALMV, values); 57} 58}59}
Test everything is OK, it's done.
Find some ideas or something not too good to write out, is their own clear enough, not enough in place?
C # serialization Linked list