public static void Go() { using (var stream = new MemoryStream()) { // 1. Construct the desired formatter IFormatter formatter = new SoapFormatter(); // 2. Construct a SurrogateSelector object SurrogateSelector ss = new SurrogateSelector(); // 3. Tell the surrogate selector to use our surrogate for DateTime objects ISerializationSurrogate utcToLocalTimeSurrogate = new UniversalToLocalTimeSerializationSurrogate(); #if GetSurrogateForCyclicalReference utcToLocalTimeSurrogate = FormatterServices.GetSurrogateForCyclicalReference(utcToLocalTimeSurrogate); #endif ss.AddSurrogate(typeof(DateTime), formatter.Context, utcToLocalTimeSurrogate); // NOTE: AddSurrogate can be called multiple times to register multiple surrogates // 4. Tell the formatter to use our surrogate selector formatter.SurrogateSelector = ss; // Create a DateTime that represents the local time on the machine & serialize it DateTime localTimeBeforeSerialize = DateTime.Now; formatter.Serialize(stream, localTimeBeforeSerialize); // The Soap-formatted stream displays the Universal time as a string to prove it worked stream.Position = 0; Console.WriteLine(new StreamReader(stream).ReadToEnd()); // Deserialize the Universal time string & convert it to a local DateTime for this machine stream.Position = 0; DateTime localTimeAfterDeserialize = (DateTime)formatter.Deserialize(stream); // Prove it worked correctly Console.WriteLine("LocalTimeBeforeSerialize={0}", localTimeBeforeSerialize); Console.WriteLine("LocalTimeAfterDeserialize={0}", localTimeAfterDeserialize); } }
public static void Go() { using (var stream = new MemoryStream()) { // 1. Construct the desired formatter IFormatter formatter = new SoapFormatter(); // 2. Construct a SurrogateSelector object SurrogateSelector ss = new SurrogateSelector(); // 3. Tell the surrogate selector to use our surrogate for DateTime objects ISerializationSurrogate utcToLocalTimeSurrogate = new UniversalToLocalTimeSerializationSurrogate(); #if GetSurrogateForCyclicalReference utcToLocalTimeSurrogate = FormatterServices.GetSurrogateForCyclicalReference(utcToLocalTimeSurrogate); #endif ss.AddSurrogate(typeof(DateTime), formatter.Context, utcToLocalTimeSurrogate); // NOTE: AddSurrogate can be called multiple times to register multiple surrogates // 4. Tell the formatter to use our surrogate selector formatter.SurrogateSelector = ss; // Create a DateTime that represents the local time on the machine & serialize it DateTime localTimeBeforeSerialize = DateTime.Now; formatter.Serialize(stream, localTimeBeforeSerialize); // The Soap-formatted stream displays the Universal time as a string to prove it worked stream.Position = 0; Console.WriteLine(new StreamReader(stream).ReadToEnd()); // Deserialize the Universal time string & convert it to a local DateTime for this machine stream.Position = 0; DateTime localTimeAfterDeserialize = (DateTime)formatter.Deserialize(stream); // Prove it worked correctly: Console.WriteLine("LocalTimeBeforeSerialize ={0}", localTimeBeforeSerialize); Console.WriteLine("LocalTimeAfterDeserialize={0}", localTimeAfterDeserialize); } }