using System; using System.Runtime.Serialization; class Person : ISerializable { public string name; public int age; public double height; public Person(string n, int a, double h) { name = n; age = a; height = h; } public Person(SerializationInfo info, StreamingContext ctxt) { name = (string) info.GetValue("Name", typeof(string)); age = info.GetInt32("Age"); height = info.GetDouble("Height"); } public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { info.AddValue("Name", name); info.AddValue("Age", age); info.AddValue("Height", height); } }
using System; using System.Runtime.Serialization; [Serializable] class MyClass { public double myDouble = 0.5; [OnDeserialized] public void OnDeserializedMethod(StreamingContext context) { Console.WriteLine("Deserialization complete."); } } class Program { static void Main(string[] args) { MyClass obj = new MyClass(); // Serialize object to a file using (var stream = new FileStream("data.xml", FileMode.Create)) { var serializer = new XmlSerializer(typeof(MyClass)); serializer.Serialize(stream, obj); } // Deserialize object from file using (var stream = new FileStream("data.xml", FileMode.Open)) { var serializer = new XmlSerializer(typeof(MyClass)); MyClass newObj = (MyClass) serializer.Deserialize(stream); Console.WriteLine($"Deserialized MyDouble value: {newObj.myDouble}"); } } }In this example, the GetDouble method is used internally by the XmlSerializer class during deserialization to retrieve the value of the "myDouble" field from the serialized data. The OnDeserialized attribute is used to perform an action after deserialization is complete. This code belongs to core .NET package library.