c# - How to serialize System.Diagnostics.Stopwatch -
I have a class named [serializable] in which the system is located. Diagnostics However, I can not serialize the class by using BinaryFormatter due to System.Diagnostics.Stopwatch. Is there a way to mark or make System.Diagnostics.Stopwatch serializable?
After
You can create a SerializationSurrogate and a surrogate selector for a stopwatch.
Be careful that the stopwatch class may have machine-specific status, i.e. tick frequency and so on. So, when you order in order, check the serialization reference that the sequence is not intended for the use of cross machine (if you intend to copy all the values), or you can create a completely new example with only time data.
Namespace mLio.Testwatch {class program {static zero main (string [] args) {container container = new container (); Container copy = null; System time to time. Serialization. Formats. Binary. Binterformor Formatter = New System From time to time. Serialization.formatars.binary.binterformer (); // Create a format elsewhere (if the formatter.SurrogateSelector == faucet) {formatter.SurrogateSelector = new StopWatchSelector (); } And {formatter.SurrogateSelector.ChainSelector (new StopWatchSelector ()); } {Formatter.Serialize (stream, container) by using System.IO.MemoryStream stream = new system.OO.MemoryStream ()); Stream.Flush (); section. Composition = 0; Copy = formatter.Deserialize (stream) as container; } System.Diagnostics.Debug.WriteLine ("Reference equals:" + (Object. Reference Aqualls (Container, Copy)). ToString ()); System.Console.ReadKey (); }} Public Class StopWatchSelector: System.Runtime.Serialization.SurrogateSelector {Private StopWatchSurrogate _Srogate; Public stopwatchcakeer () {_Sragate = new stopwatchSurGate (); } Public override System.Runtime.Serialization.ISerializationSurrogate GetSurrogate (System.Type type, System.Runtime.Serialization.StreamingContext context, out System.Runtime.Serialization.ISurrogateSelector selector) {System.Runtime.Serialization.ISerializationSurrogate rent; Surrogate = base Getsurgettes (types, references, selectors out); If (surrogate == faucet) {if (type == typeof (System.Diagnostics.Stopwatch)) {surrogate = _Sragate; }} Return surrogate; }} Public Class Stopwatch Sorogate: System. Runtime Serialization. EASERALIZATION SURGATE {PRIVATE CUT STRUCTURE NULL_INDICATOR_STRING = @ "__ stopwatch null"; // An example personal constraint System.Runtime.Serialization.StreamingContextStates INVALID_CONTEXTS = Invalid References to System.Runtime.Serialization.StreamingContextStates.CrossMachine | System.Runtime.Serialization.StreamingContextStates.Remoting; Public void GetObjectData (object obj, System.Runtime.Serialization.SerializationInfo information, System.Runtime.Serialization.StreamingContext reference) as {System.Diagnostics.Stopwatch stopwatch = obj System.Diagnostics.Stopwatch; If (stopwatch == empty) {info.AddValue (NULL_INDICATOR_STRING, true); } Other {info.AddValue (NULL_INDICATOR_STRING, incorrect); Add other values}} public Object SetObjectData (object obj, System.Runtime.Serialization.SerializationInfo information, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.ISurrogateSelector selector) seen through {SystemkDiagnostics reflection // . Stopwatch stopwalk = null; Bool isNull = info.GetBoolean (NULL_INDICATOR_STRING); If (! IsNull) {stopWatch = obj as System.Diagnostics.Stopwatch; // read other values and set through reflection} return stopwatch; } Private void CheckContext (System.Runtime.Serialization.StreamingContext context) {if ((ContextkState and INVALID_CONTEXTS)! = 0) {delete new system Denksmrthit exceptions (); }}} [System.Serializable] Public Class Container {Personal System.Diagnostics.Stopwatch _Watch = new System.Diagnostics.Stopwatch (); }}
Comments
Post a Comment